Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey fellas and/or ladies. Quick couple. I have to write a function that draws an octagon. It is 40 by 40 pixels. This function should accept arguments for x and y coordinates for the center point of the octagon. The function ultimately should draw the octagon with the center point located athose coordinates.

Next I have to call this function 6 times in order to draw a pattern.

Can anyone show me how to do this exactly? I was told by my professor to draw dotted lines horizontally and vertically in fourths. Since the radius is 40, each dotted line represents 20 of the 40. I would have to add or subract from the center point to figure out the x,y coordinates for each point. However I have no idea how to go about this. I have Visual 2010 with the DarkGDK header file which allows me to draw these shapes. I can draw up the octagon but don't know how to get those points.

This is my 2nd time posting b/c my earlier question wasn't very clear. Please and thank you :)
Posted
Updated 23-Sep-11 6:13am
v2
Comments
André Kraak 23-Sep-11 12:13pm    
It would be best if you did your own homework, it is given to you so that you will learn something. Read your text books and give it a try.
Simon Bang Terkildsen 23-Sep-11 12:37pm    
please do not repost, click "improve question" to add more details.
Can't seem to figure this function question
Richard MacCutchan 23-Sep-11 13:13pm    
Try drawing the shapes on a piece of paper so you can visualise what needs to be done to get the final result in the way you want. You can even use a ruler to get the different measurements. Converting that to code should then be much easier.

I am not sure if i understood the question correctly, but to calculate the vertices of octagon given the center(x,y) and radius R you can use this:
void Octagon(HDC hDC, int x, int y, int R)
{
int R2 = int(R/sqrt(2));

POINT pt[8];
pt[0].x = x; pt[0].y = y - R;
pt[1].x = x + R2 pt[1].y = y - R2;
pt[2].x = x + R; pt[2].y = y;
pt[3].x = x + R2; pt[3].y = y + R2;
pt[4].x = x; pt[4].y = y + R;
pt[5].x = x - R2; pt[5].y = y + R2;
pt[6].x = x - R; pt[6].y = y;
pt[7].x = x - R2; pt[7].y = y - R2;

Polygon(hDC, pt, 8);
}

(from codeguru.com/forum)

Now, you can draw any polygon (including octagon) on GDI+ with:
PointF myPointFArray[] = {PointF(1, 1), 
   PointF(20, 10), PointF(5, 4), PointF(100, 2),
   PointF(200, 50), PointF(39, 45)};
g->DrawPolygon(myPen, myPointFArray);

(Drawing Lines and Shapes with GDI+)

using sample Drawing a Line as a template.
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900