2

I have start and end coordinate of a line. I want to drawn another line sticking at the end of this this such that they will be perpendicular to each other.

I am trying to do this using the normal geometry. Is there any high-level API there in MFC for the same.

Thanks

2

4 Answers 4

7

If (dx,dy) are the differences in the x and y coodinates of the given line, you can make another line perpendicular by contriving for the differences in its coordinates to be (-dy, dx). You can scale that by any factor (-c*dy, c*dx) to change its length.

5

You have an existing line (x1, y1) to (x2, y2). The perpendicular line is (a1, b1) to (a2, b2), and centered on (x2, y2).

xdif = x2 - x1
ydif = y2 - y1
a1 = x2 - ydif / 2
b1 = y2 + xdif / 2
a2 = x2 + ydif / 2
b2 = y2 - xdif / 2

I think that works... I tested it for a few lines.

So if you have a line going from (1,1) to (5,3), the perpendicular line would be (5 - 2/2, 3+4/2) to (5 + 2/2, 3 - 4/2) or (4,5) to (6, 1).

0

You could use SetWorldTransform function from Win32 GDI API.

Sample code is here.

0

Let me add some c++ code based on kbelder answer. It make one vertex by origin point (x1,y1) and another vertex (x2,y2)

enter image description here

float GetDistance(float x1, float y1, float x2, float y2)
{
 float cx = x2 - x1;
 float cy = y2 - y1;
 float flen = sqrtf((float)(cx*cx + cy*cy));
 return flen;
}

void GetAxePoint(double x1, double y1, double x2, double y2, double& x3, double& y3, double vec_len, bool second_is_y)
{
 double xdif = x2 - x1;
 double ydif = y2 - y1;

 if(second_is_y)
 {
  x3 = x1 - ydif;
  y3 = y1 + xdif;
 }
 else
 {
  x3 = x1 + ydif;
  y3 = y1 - xdif;
 }

 double vec3_len = GetDistance(x3, y3, x1, y1);

 x3 = (x3-x1)/vec3_len;
 y3 = (y3-y1)/vec3_len;

 x3 = x1 + x3*vec_len;
 y3 = y1 + y3*vec_len;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.