468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

line drawing

I need to know the midpoint line drawing program to draw lines in all the eight directions.
Sep 17 '06 #1
1 5334
293 100+
I assume that all eight lines will have the same distance, and all have one end at the endpoint. Further, that each line will be 45 from the two closest lines.

Let r = sqrt(32). Assume that I pass (r,0) and (-r,0) as the co-ordinates. Then the other coordinates should be (0,r), (0,-r), and all four possibilities of (4,4).

If that's the case, here's what to do.
1. Find the midpoint. The x- and y-coordinates are the averages of the two x- and y-coordinates, respectively. For the example of (r,0) and (-r,0), they average to (0,0).
Expand|Select|Wrap|Line Numbers
  1. Xavg = (X1+X2)/2;
  2. Yavg = (Y1+Y2)/2;
2. Consider a line from the midpoint to either point. Describe it in polar coordinates. Let (X,Y) be either (X1,Y1) or (X2,Y2).
Expand|Select|Wrap|Line Numbers
  1. r = sqrt((X-Xavg)^2 + (Y-Yavg)^2);
  2. theta = arctan((Y-Yavg)/(X-Xavg));
3. Rotate the line by multiples of 45 degrees, which is simple to do in polar coordinates. Do a loop that goes from 0 to 7, but not 8. Then calculate a temporary theta, and derive new coordinates. r stays the same.
Expand|Select|Wrap|Line Numbers
  1. for(i = 0; i < 8; i++)
  2. {
  3.   new_theta = theta + 45*(i);
  4.   new_X = r*cos(new_theta);
  5.   new_Y = r*sin(new_theta);
  6.   // Draw line from (Xavg,Yavg) to (new_X,new_Y)
  7. }
Sep 17 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Dennis | last post: by
reply views Thread by Tomi Holger Engdahl | last post: by
9 posts views Thread by Steve Long | last post: by
6 posts views Thread by Rene | last post: by
4 posts views Thread by Mathieu Chavoutier | last post: by
2 posts views Thread by Martijn Mulder | last post: by
13 posts views Thread by Martijn Mulder | last post: by
2 posts views Thread by mattc66 via AccessMonster.com | last post: by
5 posts views Thread by Macias | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.