By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

line drawing

P: 1
I need to know the midpoint line drawing program to draw lines in all the eight directions.
Sep 17 '06 #1
Share this Question
Share on Google+
1 Reply

P: 293
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.