🔒 Locked
Help Drawing Lines
Advertisement
Original Post
Second hit on Google.
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.
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.
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)
y = x * tan(angle)
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.
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.
Quote:
Original post by khdani
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.
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; } }}Essentially you know the X or Y value and the angle, so you can work out the missing value.
That's C++ code...try porting it in C#
void Graphic::FillRect(RECT Rect, float radinc)
{
POINT Pt;
POINT Center;
float alfa = 0.0f;
float archeight = atan(( (Rect.bottom-Rect.top)/2 ) / ( (Rect.right-Rect.left)/2));
//point at the center of the rectangle
Center.x = LONG(Rect.left + ((Rect.right-Rect.left)/2));
Center.y = LONG(Rect.top + ((Rect.bottom-Rect.top)/2));
Pt.x = Rect.right;
for (; alfa < archeight; alfa += radinc)
{
Pt.y = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
Pt.y = Rect.top;
for (; alfa < (Math.Pi-archeight); alfa += radinc)
{
Pt.x = LONG(floor(Rect.top / tan(alfa)));
Graphic.DrawLine(Center, Pt);
}
Pt.x = Rect.left;
for (; alfa < (Math.Pi+archeight); alfa += radinc)
{
Pt.x = LONG(floor(Rect.top / tan(alfa)));
Graphic.DrawLine(Center, Pt);
}
Pt.y = Rect.bottom;
for (; alfa < ((2*Math.Pi)-archeight); alfa += radinc)
{
Pt.x = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
Pt.x = Rect.right;
for (; alfa < (2*Math.Pi); alfa += radinc)
{
Pt.y = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
}
radinc is the angular increment (in radians!)
void Graphic::FillRect(RECT Rect, float radinc)
{
POINT Pt;
POINT Center;
float alfa = 0.0f;
float archeight = atan(( (Rect.bottom-Rect.top)/2 ) / ( (Rect.right-Rect.left)/2));
//point at the center of the rectangle
Center.x = LONG(Rect.left + ((Rect.right-Rect.left)/2));
Center.y = LONG(Rect.top + ((Rect.bottom-Rect.top)/2));
Pt.x = Rect.right;
for (; alfa < archeight; alfa += radinc)
{
Pt.y = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
Pt.y = Rect.top;
for (; alfa < (Math.Pi-archeight); alfa += radinc)
{
Pt.x = LONG(floor(Rect.top / tan(alfa)));
Graphic.DrawLine(Center, Pt);
}
Pt.x = Rect.left;
for (; alfa < (Math.Pi+archeight); alfa += radinc)
{
Pt.x = LONG(floor(Rect.top / tan(alfa)));
Graphic.DrawLine(Center, Pt);
}
Pt.y = Rect.bottom;
for (; alfa < ((2*Math.Pi)-archeight); alfa += radinc)
{
Pt.x = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
Pt.x = Rect.right;
for (; alfa < (2*Math.Pi); alfa += radinc)
{
Pt.y = LONG(floor(tan(alfa) * Rect.right));
Graphic.DrawLine(Center, Pt);
}
}
radinc is the angular increment (in radians!)
well...I'm a bit late...use the code you like the most. Good work!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement