Reputation: 16900
I want to draw a line from point A to point B. However the lines itself should be intelligent in the sense that if point B is exactly
below point A a straight line should get drawn. But if the point B is below A and little far horizontally from A then a line should get drawn in right angled manner. I hope you are getting me. If you may have used any UML tool like edraw Max or any other you may have seen these types of lines. Any idea how can we achieve this?
Thanks in advance :)
Upvotes: 5
Views: 243
Reputation: 886
Is this what you mean by right angled intelligence? pseudo ensue...
Point pA(x,y);
Point pB(x,y);
if abs(pB.X-pA.X) < abs(pB.Y-pA.Y) // Going vertically or horizontal?
{
DrawLine(pA.X, pA.Y, pA.X, pB.Y); //Long vertical
DrawLine(pA.X, pB.Y, pB.X, pB.Y); //Short horizontal
}
else
{
DrawLine(pA.X, pA.Y, pB.X, pA.Y); //Long horizontal
DrawLine(pB.X, pA.Y, pB.X, pB.Y); //Short vertical
}
or for the crooked line (off the top of my head):
Point pA=(x,y);
Point pB=(x,y)
if abs(pB.X-pA.X) < abs(pB.Y-pA.Y) // Going vertically or horizontal?
{
Point pHalfwayY = (pB.Y-pA.Y)/2 + pB.Y
DrawLine(pA.X, pA.Y, pA.X, pHalfwayY ); //Long vertical 1st half
DrawLine(pA.X, pHalfwayY , pB.X, pHalfwayY ); //Short horizontal
DrawLine(pA.X, pHalfwayY , pA.X, pB.Y); //Long vertical 2nd half
}
else
{
Point pHalfwayX = (pB.X-pA.X)/2 + pB.Y
DrawLine(pA.X, pA.Y,pHalfwayX , pA.Y); //Long horizontal 1st Half
DrawLine(pHalfwayX , pA.Y, pHalfwayX , pB.Y); // Short Vertical
DrawLine(pHalfwayX , pA.Y, pA.X, pB.Y); //Long horizontal 2nd half
}
Hope this helps.
Upvotes: 1
Reputation: 11732
Here's some code:
void connectPoints(Point a, Point b)
{
Point middlePoint1(a.x, (a.y + b.y)/2);
Point middlePoint2(b.x, (a.y + b.y)/2);
drawLine(a, middlePoint1);
drawLine(middlePoint1, middlePoint2);
drawLine(middlePoint2, b);
}
To clarify, the asker actually wants 3-segment axis-aligned lines that look like most connections here:
Upvotes: 4
Reputation: 17274
What's the problem with straightforward approach?
// pA, pB - points
DrawLine(pA.X, pA.Y, pA.X, pB.Y); // vertical line from A point down/up to B
DrawLine(pA.X, pB.Y, pB.X, pB.Y); // horizontal line to B
Upvotes: 1
Reputation: 5968
Graphics libraries like GDI+ will handle that for you, and it will draw the line according to its starting and ending points.
If you want to handle this your self, you have to work with triangular math to determine the rotation angle of your line.
Upvotes: 0