# Help Drawing Lines

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

## Recommended Posts

Hello, How to draw a line from a center of rectangle to edges of that rectangle every, say, 15 degrees ? Thank You

##### Share on other sites

What platform, what language, what API etc etc. There are 2353 ways to draw lines so help us out in knowning which one you want.

##### Share on other sites
It's C#, using GDI+.

##### Share on other sites

Also, you tend not to specify lines as position and direction, more start and end points. You can calculate the end point from an angle with some basic trigonometry though.

EDIT: With GDI in C++, the function is LineTo. I imagine the C# / GDI+ equivalent will be similar and easy to find on MSDN / Google.

##### Share on other sites
The problem is not to draw the line, but to find the end points...
I know it's really a simple solution but I just can't remember the math of that.

##### Share on other sites
Well, if you know the X coordinate of the target point (the edge of the rectangle), then you can work out the Y coordinate from the angle with:
y = x * tan(angle)

##### Share on other sites
you mean dy=dx*tan(alpha) ?
anyway i don't have the x coordinate.
I've a rectangle with its (left,top) and (right, bottom), i've the center of the rectangle. I need to draw 24 lines, one every 15 degrees, from the center to the edge of the rectangle.

i can do:
line(cx,cy,cx+cos(alpha),cy+sin(alpha))
it will make the lines which i need but they won't reach the edges of the rectangle but the perimeter of the surrounded circle. i need them to reach the edges of the rectangle.

##### Share on other sites
If the math is over your head you could try masking. I don't know if GDI+ is capable of that though. If GDI+ has no masking functions you could manually plot your line with pixels and check to see if your pixel is inside the rectangle.

##### Share on other sites
The dimensions and the position of the rectangle is not static.

##### Share on other sites
Quote:
 Original post by khdaniyou mean dy=dx*tan(alpha) ?anyway i don't have the x coordinate.I've a rectangle with its (left,top) and (right, bottom), i've the center of the rectangle. I need to draw 24 lines, one every 15 degrees, from the center to the edge of the rectangle.i can do:line(cx,cy,cx+cos(alpha),cy+sin(alpha))it will make the lines which i need but they won't reach the edges of the rectangle but the perimeter of the surrounded circle. i need them to reach the edges of the rectangle.

C++ code, but should convert to C# easily enough:
POINT ptTopLeft{10, 20};       // Top left corner of rectPOINT ptBottomRight{50, 200};  // Bottom right corner of rectSIZE sizeRect;sizeRect.cx = ptBottomRight.x - ptTopLeft.x;sizeRect.cy = ptBottomRight.y - ptTopLeft.y;POINT ptCenter;ptCenter.x = sizeRect.cx/2 + ptTopLeft.x;ptCenter.y = sizeRect.cy/2 + ptTopLeft.y;for(int nAngle=0; nAngle<=360; nAngle += 15){   POINT ptTarg;   // Special case for vertical lines   if(nAngle == 0)   {      ptTarg.x = ptCenter.x;      ptTarg.y = ptTopLeft.y;   }   else if(nAngle == 180)   {      ptTarg.x = ptCenter.x;      ptTarg.y = ptBottomRight.y;   }   else   {      // Find Y-intersection point with edge      int y = tan(nAngle) * sizeRect.cx/2;      // Out of bounds?      if(y > sizeRect.cy)      {         // Intersection happens on top or bottom edge         ptTarg.x = (sizeRect.cy/2) / tan(nAngle);         ptTarg.y = sizeRect.cy/2;      }      else      {         // Intersection happens on left or right edge         ptTarg.x = sizeRect.cx/2;         ptTarg.y = y;      }   }}
That's just a very rough stab at it, I don't have time to properly test it just now. You'll also want to convert all angles to radians.

Essentially you know the X or Y value and the angle, so you can work out the missing value.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633385
• Total Posts
3011600
• ### Who's Online (See full list)

There are no registered users currently online

×