# [c++] representing shapes using 2D arrays

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I'm just wondering, are there any efficient, general ways to store shapes (mainly circles) using 2D char arrays? Sorry for the bad explanation, for example if I wanted to store a 'circle' of radius 2 (2 bytes), the 2d array (4 by 4 bytes) should look like: 0, 1, 1, 0 1, 1, 1, 1 1, 1, 1, 1 0, 1, 1, 0 What I'm looking for is a way to automatically generate the above 2d array for a 'circle' of any radius, so my function prototype would look something like: void genCircle(int radius, char circle[][MAX_SIZE]); and after the function is finished circle array will be filled with bytes set to 1 accordingly. Any help is appreciated.

##### Share on other sites
The hard part is calculating the positions of the points within the bounds of an array... that is, in order to get an acurate circle you would need to calculate the positions of of it's points (there are a great many ways to do this.... that isnt the issue). But this operation would risk an out-of-bounds error if not properly written (not to mention the fact that it would be rather slow).

Might I ask why you need this sort of routine? perhaps there is a better way to accomplish the same goal without the old style bitmap generation (perhaps using an actual bitmap file, perhaps).

<note to self: wow, i just noticed -- i really have to do something about my user rating :-) >

##### Share on other sites
So, if you have a 4x4 grid as in your example:

0, 1, 1, 0
1, 1, 1, 1
1, 1, 1, 1
0, 1, 1, 0

Just consider it a cartesian graph (i.e. shift your indices):

0, 1,|1, 0
1, 1,|1, 1
---------
1, 1,|1, 1
0, 1,|1, 0

Now, for every X and Y, you can find its distance from the origin via the Pythagorean Theorem:

dist = sqrt( X^2 + Y^2 )

If for a coordinate (X,Y), that distance comes out to be < your radius (or <=, your choice), then make it a '1'. Else, make it a '0'.

If you need help with C/C++ semantics as well, just let it be known!

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×