Calculate rectangle width and height from diagonal and rotation - geometry

I have a rotate rectangle and I know the size of the diagonal. I also know the angle used to rotate the rectangle.
How can I calculate the width and height of the rectangle?
For a sketch of the problem, see:

1) create a new line starting at one of the end-points of the diagonal and travelling at the rotation angle.
2) project the other diagonal terminus onto this line. You now know one side of the rectangle.
3) Copy the segment to the other side of the diagonal and connect the endpoints to complete the rectangle.
The only 'tricky' code here is the projection. This webpage has some example code for Point-Line distance/projection: http://softsurfer.com/Archive/algorithm_0102/algorithm_0102.htm

Thanks David Rutten,
I got it working. Your site about the projection was to much for my math knowledge, but with some google i find a nice point to line intersection function which helped me to calc the length (distance) of one of the sides.
Unfortunately I'm too "new" here to award you with credits or reply on your anwser.
#Eric bainville: I knew the distance because i had point1 and point2 (upper left and bottom right) coordinates. With those coordinates it is possible. I didnt mention this, but luckily David guessed right that I knew them.
Thanks again!

Related

Algorithm for regularizing or normalizing a polygon which is supposed to be rectangular

My app captures the shape of a room by having the user point a camera at floor corners, and then doing a bunch of math, eventually ending up with a polygon.
The assumption is that the walls are straight (not curved). The majority of the corners are formed by walls at right angles to each other, but in some cases might not be.
Depending on how accurately the user points the camera, the (x,y) coordinates I derive for the corner might be beyond the actual corner, or in front of the actual camera, or, less likely, to the left or right. Obviously, in this case, when I connect the dots, I get weird parallelogram or rhomboid shapes. See example.
I am looking for a program or algorithm to normalize or regularize these shapes, provided we know which corners are supposed to be right angles.
My initial attempt involved finding segments which had angles which were "close" to each other, adjust them all to the same angle, and then recalculate the vertices. However, this algorithm proved to be unstable.
My current thinking is to find angles which are most obtuse (as would be caused by a point mistakenly placed beyond the actual corner), or most acute (as would be caused by a point mistakenly placed in front of the actual corner), and find the corner point which would make it a right angle. The problem, however, is that such as adjustment could have side-effects on other corners, such as making them even further away from right angles. I sense I need some kind of algorithm which takes all the information and optimizes/solves it at once--is this a kind of linear programming problem?--but I am stuck.
There is not a unique solution.
For example, take the perpendicular from the middle point of an edge to the two neighboring edges. This will give you two new corners.
Or take the perpendicular from the end point of an edge to other edges.
Or compute the average of angles in the end points of an edge. Use this average and the middle point of the edge to compute new corners.
Or...
To get the most faithful compliance, capture (or calculate) distances from each corner to the other three. Build triangles with those distances. Then use the average of the coordinates you compute for a corner from 2 or 3 triangles.
Resulting angles will not be exactly 90 degrees, but the polygon will represent the room fairly.

What is the reference point for measuring angles in OpenCV?

I'm trying to infer an object's direction of movement using dense optical flow in OpenCV. I'm using calcOpticalFlowFarneback() to get flow coordinates and cartToPolar() to acquire vector angles which would indicate direction.
To interpret the results I need to know the reference point for measuring the angle. I have found this blog post indicating that the range of angles is 360°. That tells me that the angle measurement would go along the lines of the unit circle. I couldn't make out much more than that.
The documentation for cartToPolar() doesn't cover this and my attempts at testing it have failed.
It seems that the angle produced by cartToPolar() is in reference to the unit circle rotated clockwise by 90° centered on the image coordinate starting point in the top left corner. It would look like this.
I came to this conclusion by using the dense optical flow example provided by OpenCV. I replaced the line hsv[...,0] = ang*180/np.pi/2 with hsv[...,0] = ang*180/np.pi to get correct angle conversion from radians. Then I tested a video with people moving from top right to bottom left and vice versa. I sampled the dominant color with GIMP and got RGB values which I converted to HSV values. Hue value corresponds to the angle in degrees.
People moving from top right to bottom left produced an angle of about 300° and people moving the other way round produced an angle of about 120°. This hinted at the way the unit circle is positioned.
Looking at the code, fastAtan32f is used to compute the angles. and that seems to be a atan2 implementation.

Find degrees (0-360º) of point on a circle

I'm working on a small webapp in which I need to rotate shapes. I
would like to achieve this by grabbing a point on a circle and
dragging it around to rotate the image.
Here's a quick illustration to help explain things:
My main circle can be dragged anywhere on the canvas. I know it's
radius (r) and where 12 o'clock (p0) will always be (cx, cy - r). What
I need to know is what degree p1 will be (0-360º) so I can rotate the
contents of the main circle accordingly with Raphael.rotate().
I've run through a bunch of different JavaScript formulations to find this (example), but none seem to give me values between 0-360 and my basic math skills
are woefully deficient.
The Color Picker demo (sliding the cursor along the ring on the right) has the behavior I want, but even after poring over the source code I can't seem to replicate it accurately.
Anything to point me in the correct direction would be appreciated.
// Angle between the center of the circle and p1,
// measured in degrees counter-clockwise from the positive X axis (horizontal)
( Math.atan2(p1.y-cy,p1.x-cx) * 180/Math.PI + 360 ) % 360
The angle between the center of the circle and p0 will always be +90°. See Math.atan2 for more details.

How to determine if a vertical line segment intersects a circle?

This is a programming homework assignment, of which I have no qualms about doing it myself however I'm quite stuck on the geometry of it. I need to be able to determine the exact point of intersection given the center and radius of a circle and two end points of a vertical line segment, and since geometry isn't my forte I was hoping for some help (even pointers in the right direction would be appreciated!)
This probably isn't the best place to ask a question like this but I'm not really sure where else to look for help, my apologies if it's against the rules or something.
edit:
My apologies, what I am really having trouble with is determining what the points of intersection are (and if there is one intersection or two.) I've tried each solution given and they work great for determining if there is an intersection or not but my problem still persists as I mis-worded my question. If anyone can help with that it'd be much appreciated!
Try http://mathworld.wolfram.com/Circle-LineIntersection.html, this covers the geometry aspect of your problem quite well.
If C=(x0,y0) is the center, r the radius, and k the abscissa of the line, you have
y = y0 +/- sqrt(r^2-(k-x0)^2), but no intersection if r < abs(k-x0)
using the centre [x,y] of the circle, find the distance of this particular line from the centre.refer
now if this distance is > radius of the circle => the line won't intersect. otherwise, it will.

how to determine polygon rotation angle

I am writing a program (.net) to create a stadium style layout and need to determine the angle of rotation for each polygon compared to the horizontal.
This is so i can construct the contents of the polygon and also rotate this correctly to fit inside.
Given the below image as an example to simulate each variant of the facing direction (indicated by the red line) how could i determine the the rotation angle needed to get the shape to have the red line on top as is already shown by shape 5.
http://i40.tinypic.com/16ifhoo.gif
I have found logic to determine the angle of the points that make up the red line, but I also need to know the rotation to get it back to horizontal.
I'm not sure if i need some central reference point for all polygons to help.
How could I best solve this?
If you know the angle of the red line for some polygon (a, say), then the polygon is on one side or other of that line. So:
Use the average colour of some pixels near the line on both sides to determine which is the case.
If the polygon is above the line, the rotation angle is 180+a.
If the polygon is below the line, the rotation is a.
where above and below correspond to the smaller-angle side and larger-angle sides of the line according to how you measure a.
I would try to calculate the normal vectors on each red line (eg. 0 degrees for polygon 5, 45 degrees for 4, 90 degrees for 3, etc.) and then the angle you need to rotate that normal - and thus the matching polygon - so that the normal "points up" should be very simple.
Unfortunately I don't have the needed formulae available for you off the top of my head, but Googling "normal vector" and/or searching for it on Wikipedia should get you started just fine, I think. Possibly in the direction of the so called 'cross product'.
No central reference point for all polygons should be needed for this (normal direction is not related to absolute coordinates).
sin, cos, tan functions allow you to convert from triangle edge ratio to degrees.
Imagine, one end of red line is at (x1,y1) and other end is at (x2,y2). You can treat red line as hipotenuse of rectangular triangle and use arctan to get degrees.
Ratio between catheti is (x2-x1) / (y2 - y1). Rotation of red line then is arctan((x2-x1) / (y2 - y1)). Watch out for situations when y1-y1 is 0!
Let's try one example from your picture, polygon 6 with coords (55, 65) and (65, 55). Type in google: "arctan((65-55)/(55-65)) in degrees"

Resources