0
votes

In my application user points two points PointA and PointB on the same row (could be at any angle). So I have the following information

  1. PointA coordinates

  2. pointB coordinates

  3. Distance between PointA and Point B

  4. An Across distance (taken from user as input to draw other points)

  5. Angle (calculated from pointA and pointB).

Based on this information, another application draws four points (vertices of rectangle).

What I have to do is, I have to find the centre point of those four points (rectangle) to be able to properly draw the rectangle bounded between those 4 points.

Right now I am able to draw the rectangle taking the centre as the pointA which obviously is incorrect. What formula should I use to calculate the centre of Rectangle so that I can draw a bounded rectangle?

Image 1:

enter image description here

Image 2:

enter image description here

Image 3:

enter image description here

Image 4:

enter image description here

Image 5:

enter image description here

As seen in the attached images, in every case rectangle is getting drawn with pointA as centroid. While I want the centroid to the centre of the FOUR points.

P.S: All angles are measured 0 degrees North.

2
Why do you need a center of those four points in order to draw the rectangle, when you can use the four points to draw it?AgentFire
I dont have the four points. Only information I have is point 1-5 mentioned above.WAQ
Which angle? A, B and ...? Three points for an angle! Probably adding a sketch is the best option for us and you to understand!Sigi
Do you have coordinates of point 4?AgentFire
No, I have coordinates of only P1 and P2 (both on same row). I do have the height though.WAQ

2 Answers

0
votes

I think:

Let P0 = {x0,y0} and P1 = {x1,y1}

Let the vector V01 = P1 - P0 == {V01x = P1x - P0x, V01y = P1y - P0y}

Let the vector V03 = {V01x * Cos(PI/2) - V01y * Sin(PI/2) , V01x* Sin(PI/2) + V01y * Cos(PI/2)

Width = Sqrt(V03x * V03x + V03y * V03y)

VN = V03 / Width == {V03x / Width, V03y / Width}

P3 = P0 + VN * Height

P4 = P1 + VN * Height

PC = (P0 + P1 + P2 + P3) / 4

0
votes

In case you have two points p1, p2 and you need to draw a rectange (get other 2 points a1, a2) from these:

a1.x = p1.x;
a1.y = p2.y;
a2.x = p2.x;
a2.y = p1.y;

There you go, four points p1, a2, p2, a1 (clockwise order) describe your rectangle.


Update:

var width = p2.x - p1.x;
var height = p2.y - p1.y;
var angle = 0;
var center = new Point(p1.x + width / 2, p1.y + height / 2);

Update:

var center = new Point();
var angle = //you have it. Radians.
var height = // you have this as well.
var halfSegment = new Point((p2.x - p1.x) / 2, (p2.y - p1.y) / 2);
center.x = halfSegment.x + Math.Cos(angle - Math.PI / 2) * height / 2;
center.y = halfSegment.y + Math.Sin(angle - Math.PI / 2) * height / 2;