Calculating Coordinates on Screen - coordinates

I know there are a load of other questions on this topic, I think I've read most of them, along with Wikipedia and a bunch of other articles but I am missing (I think) some simple arithmetic to complete my coordinate calculations.
I have this code:
typedef struct {
double startX;
double startY;
double x2;
double y2;
double length;
double angle;
double lastAngle;
} LINE;
void lineCalc(LINE *lp) {
double radians = lp->angle * 3.141592653589793/180.0;
lp->x2 = lp->startX + (lp->length * cos(radians));
lp->y2 = lp->startY + (lp->length * sin(radians));
fprintf (stderr, "lineCalc:startX:%2.3f, startY:%2.3f, length:%2.3g, angle:%2.3f, cos(%2.3f):%2.3f, x2:%2.3f, y2:%2.3f\n", lp->startX, lp->startY, lp->length, lp->angle, lp->angle, cos(radians), lp->x2, lp->y2);
}
int main() {
// Initialise to origin of 250, 250. 0, 0 for initial end point. Length 150, first angle 60 (degrees), l.lastAngle currently not used
LINE l = {250, 250, 0, 0, 150, 60, 0};
lineCalc(&l);
//drawLine(&l);
l.startX = x2; l.startY = y2; // make last end point, new start point. Angle stays at 60 degrees
lineCalc(&l);
//drawLine(&l);
l.startX = x2; l.startY = y2;
lineCalc(&l);
//drawLine(&l);
}
Which calculates the end point of a line given its start point, length and angle. All fine and good, but what I want to be able to do is to draw a shape, a triangle would be a start.
At the moment the code will make the calculation, draw the line (in reality it is generating SVG), make the last end point the next origin, recalculate, draw the next line etc...
The crucial bit that I am missing is how to get angle to be relative to the last line drawn. At the moment, the moving of the origin works fine, but the angle stays the same, thus three lines with angles of 60 degrees will just draw a straight line because the angle is relative to the start rather than relative to the last line.
Just in case it is relevant, with SVG horizontal is zero degrees. Thus a line 50 units long, starting at y100, x100 at an angle of 90 degrees will have an end point of y150, x100.
Could someone point out the obvious thing that I a missing to make the angles correct relative to the last line please?

If you take the angle at which the first line is drawn at as theta:
theta + 180 deg OR theta - 180 deg will face you back down the line you just drew.
Then theta + 180 deg + 60 OR theta - 180 deg + 60 will face you at 60 degrees to the first line.
You need to choose whether to + or - the 180 based on the range of degrees that svg uses (does it go -180 to 180 or 0 to 360) and how big your starting theta is. Also you need to choose + or - 60 degrees based on the side of the first line that you want to draw the second line.
Once you've calculated the angle you're drawing the second line at (theta + 180 + 60 for instance) then you need to take that as your next theta to calculate the angle for the third line.

Related

point projection into yx rotated plane

I want to simulate depth in a 2D space, If I have a point P1 I suppose that I need to project that given point P1 into a plane x axis rotated "theta" rads clockwise, to get P1'
It seems that P1'.x coord has to be the same as the P1.x and the P1'.y has to b shorter than P1.y. In a 3D world:
cosa = cos(theta)
sina = sin(theta)
P1'.x = P1.x
P1'.y = P1.y * cosa - P1.z * sina
P1'.z = P1.y * sina + P1.z * cosa
Is my P1.z = 0? I tried it and P1'.y = P1.y * cosa doesn't result as expected
Any response would be appreciated, Thanks!
EDIT: What I want, now I rotate camera and translate matrix
EDIT 2: an example of a single line with a start1 point and a end1 point (it's an horizontal line, result expected is a falling line to the "floor" as long as tilt angle increases)
I think it's a sign error or an offset needed (java canvas drawing (0,0) is at top-left), because my new line with a tilt of 0 is the one below of all and with a value of 90º the new line and the original one match
The calculation you are performing is correct if you would like to perform a rotation around the x axis clockwise. If you think of your line as a sheet of paper, a rotation of 0 degrees is you looking directly at the line.
For the example you have given the line is horizontal to the x axis. This will not change on rotation around the x axis (the line and the axis around which it is rotating are parallel to one another). As you rotate between 0 and 90 degrees the y co-ordinates of the line will decrease with P1.y*cos(theta) down to 0 at 90 degrees (think about the piece of paper we have been rotating around it's bottom edge, the x axis, at 90 degrees the paper is flat, and the y axis is perpendicular to the page, thus both edges of the page have the same y co-ordinate, both the side that is the "x-axis" and the opposite parallel side will have y=0).
Thus as you can see for your example this has worked correctly.
EDIT: The reason that multiplying by 90 degrees does not give an exactly zero answer is simply floating point rounding

Get x,y point from angle and radius

I need to get a specific point with specified angle and distance(Radius) from center point of my base UIView. I found this question How to calculate a point with an given center, angle and radius? but this doesn't give me correct point. I'm using below code to find the point,
Basically i need to find the x,y point from 100px distance and 90 degrees(clock wise) from center point of my base view.
int angle = 90 * M_PI / 180;//need to find the point of 90 degrees
int distance = 100;
int line_end_x = 160 + cos(angle)*distance;//160 is my center x
int line_end_y = 274 + sin(angle)*distance;//274 is my center y
myView.center = CGPointMake(line_end_x, line_end_y);
Above code gives me below output,
Red box is "myView" with (20px x 20px)
May be someone can tell me whats wrong i'm doing?
I doubt that you want your angle to be expressed as an int.
Your angle is Pi/2 which is 1.57.
Maybe try float angle = 90 * M_PI / 180;

Point tangent to circle

Using this as a [reference][1]: Find a tangent point on circle?
cx = 0;
cy = 0;
px = -3;
py = -8;
dx = cx - px;
dy = cy - py;
a = asin(5 / ((dx*dx + dy*dy)^0.5));
b = atan2(dy, dx);
t_1 = deg2rad(180) + b - a;
t_2 = deg2rad(180) + b + a;
For a point (7,6) the angles are 7.9572/73.4434 and for (-3, -8) are 213.6264/285.2615. So for the first quadrant, the angles do not make sense, but for third quadrant they do. What I am doing wrong?
Your formula for a is wrong. You should use
a = acos(5 / ((dx*dx + dy*dy)^0.5))
instead of
a = asin(5 / ((dx*dx + dy*dy)^0.5))
i.e. use acos(...) instead of asin(...). The reason is shown in the image below. The formula for angle a is a=acos(r/H), where r is the radius of the circle and H is the length of the hypotenuse of the right angle triangle. So this has nothing to do with the fact that asin(...) has no way to know which of the two possible quadrants the value that is passed in lies. the argument of the asin is always positive, and you always want the answer in the range 0 to 90 degrees.
So the answer for the two angles that you want are b+a and b-a. Using acos instead of asin in your two cases produces 97.7592 & -16.5566 (or equivalently 343.4434) for your first quadrant example, and -164.7385 & -56.3736 (or equivalently 195.2615 and 303.6264) for your third quadrant example. (NB: instead of adding 180 degrees in the formula for t_1 and t-2, you could just switch the signs of dx and dy)
First -- I spent like 10 minutes figuring out what the heck you're trying to do (which in the end, I got from a comment in one of the answers), while solving your problem took 2 minutes. So, for future reference, please give a description of your problem as clear as you can first.
Now, I think you just have your signs messed up. Try the following:
%// difference vector
%// NOTE: these go the other way around for the atan2 to come out right
dx = px - cx;
dy = py - cy;
%// tip angle of the right triangle
a = asin( 5 / sqrt(dx*dx + dy*dy) );
%// angle between the (local) X-axis and the line of interest
b = atan2(dy, dx);
%// the third angle in the right triangle
%// NOTE: minus a here instead of plus b
g = pi/2 - a;
%// Angles of interest
%// NOTE1: signs are flipped; this automatically takes care of overshoots
%// NOTE2: don't forget to mod 360
t_1 = mod( rad2deg(b - g), 360)
t_2 = mod( rad2deg(b + g), 360)
Alternatively, you could skip computing the intermediate angle a by using acos instead of asin:
%// difference vector
dx = px - cx;
dy = py - cy;
%// Directly compute the third angle of the right triangle
%// (that is, the angle "at the origin")
g = acos( 5 / sqrt(dx*dx + dy*dy) );
%// angle between the (local) X-axis and the line of interest
b = atan2(dy, dx);
%// Angles of interest
t_1 = mod( rad2deg(b - g), 360)
t_2 = mod( rad2deg(b + g), 360)
Just another wayto re-discover the trigonometric identity acos(x) = pi/2 - asin(x) :)
This MathWorld entry is what you want: http://mathworld.wolfram.com/CircleTangentLine.html.
Alright, it looks like you are not accounting for the fact that asin, atan, ( any a-trig function ) has no way to know which of the two possible quadrants the value you passed in lies. To make up for that, a-trig function will assume that your point is in the first or fourth quadrant ( northeast / southeast ). Therefore, if you call atan function and your original point was in the second or third quadrant, you need to add 180 degrees / pi radians onto whatever value it returns.
See the documentation here stating that asin returns a value from [-pi/2, pi/2] :
http://www.mathworks.com/help/matlab/ref/asin.html
Hope that helps :)
EDIT
I misunderstood the situation originally
Here is what I think you have calculated :
t_1 and t_2 represent the angles you would travel at if you started on the circle from the tangent point and wanted to travel to your original starting point.
Viewed with this perspective your angles are correct.
For the point (7,6)
If you started on the circle at approx. (0,5) and traveled at 7 degrees, you would hit the point.
If you started on the circle at approx. (5,0) and traveled at 70 degrees, you would hit the point.
Now, what is going to be more useful and less confusing than angles, will be to know the slope of the line. To get this from the angle, do the following with angle in degrees:
angle = (angle + 90 + 360) % 180 - 90 // this gives us the angle as it would be in quad 1 or 4
slope = tan( deg2rad( angle ) )

How to detect a strict clockwise/ counter clockwse motion in MATLAB

I need to write a small program which to test whether a line (position vector) does strictly clockwise or CCLW movement. I tried to use atand to find the angle, but it could jump from negative to positive value when it pass thought 90 deg, it will have the same thing if I use slope method.
However, the motion does not have to cut at 90 deg, it could jump from 89 to 91. Then a big slope jump could happen. Any idea please
Thanks
One way to do this would be to calculate the cross-product of consecutive position vectors. If all the cross-products are positive then the line moved with a strictly clockwise. Similarly, if they are all negative then the line moved counter-clockwise. If the signs are mixed then the line did not move strictly in one angular direction:
function checkRotation(pos)
pos(:,3) = 0;
pos = unique(sum(sign(cross(pos(1:end-1,:), pos(2:end,:))), 2));
if isequal(pos, 1)
disp('Rotation was counter-clockwise');
elseif isequal(pos, -1)
disp('Rotation was clockwise');
else
disp('No strict rotation direction');
end
Create some random position vectors on -10<=x<=10 and -10<=y<=10 and test rotation:
>> pos = 20 * rand([10, 2]) - 10
pos =
-8.28968405819912 9.26177078573826
-4.75035530603335 0.936114374779359
6.02029245539477 0.422716616080031
-9.41559444875707 -5.36811226582952
8.57708278956089 -0.222045121596661
4.60661725710906 2.48120176347379
-0.227820523928417 3.58271081731495
1.57050122046878 -2.08969568662814
-5.25432840456957 -2.65126702911047
-0.823023436401378 9.75964006323266
>> checkRotation(pos)
No strict rotation direction
Create position vectors that move only CCW and test:
>> theta = 0:15:180;
>> pos = [cosd(theta)' sind(theta)'];
>> checkRotation(pos)
Rotation was counter-clockwise
and similarly for CW rotation:
>> theta = 180:-15:0;
>> pos = [cosd(theta)' sind(theta)'];
>> checkRotation(pos)
Rotation was clockwise
Note that the success of rotation detection is limited by your sampling rate. If the line rotates counter-clockwise by more than 180 degrees in successive samplings of the line position, it is indistinguishable from a rotation less than 180 degrees in the clockwise direction. This is an example of aliasing.

Is there a fast way to calculate the smallest delta between two rotation values?

There are two views:
viewA and viewB. Both are rotated.
The coordinate system for rotation is weird: It goes from 0 to 179,999999 or -179,99999 degrees. So essentially 179,99999 and -179,99999 are very close together!
I want to calculate how much degrees or radians are between these rotations.
For example:
viewA is rotated at 20 degrees
viewB is rotated at 30 degrees
I could just do: rotationB - rotationA = 10.
But the problem with this formula:
viewA is rotated at 179 degrees
viewB is rotated at -179 degrees
that would go wrong: rotationB - rotationA = -179 - 179 = -358
358 is plain wrong, because they are very close together in reality. So one thing I could do maybe is to check if the absolute result value is bigger than 180, and if so, calculate it the other way around to get the short true delta. But I feel this is plain wrong and bad, because of possible floating point errors and unprecision. So if two views are rotated essentially equally at 179,99999999999 degrees I might get a weird 180 or a 0 if I am lucky.
Maybe there's a genius-style math formular with PI, sine or other useful stuff to get around this problem?
EDIT: Original answer (with Mod) was wrong. would have given 180 - right answer in certain circumstances (angles 30 and -20 for example would give answer of 130, not correct answer of 50):
Two correct answers for all scenarios:
If A1 and A2 are two angles (between -179.99999 and 179.99999,
and Abs means take the Absolute Value,
The angular distance between them, is expressed by:
Angle between = 180 - Abs(Abs(A1 - A2) - 180)
Or, using C-style ternary operator:
Angle between = A1 < 180 + A2? A1 - A2: 360 + A1 - A2
Judging from the recent questions you've asked, you might want to read up on the unit circle. This is a fundamental concept in trigonometry, and it is how angles are calculated when doing rotations using CGAffineTransforms or CATransform3Ds.
Basically, the unit circle goes from 0 to 360 degrees, or 0 to 2 * pi (M_PI is the constant used on the iPhone) radians. Any angle greater than 360 degrees is the same as that angle minus a multiple of 360 degrees. For example, 740 degrees is the same as 380 degrees, which is the same as 20 degrees, when it comes to the ending position of something rotated by that much.
Likewise, negative degrees are the same as if you'd added a multiple of 360 degrees to them. -20 degrees is the same as 340 degrees.
There's no magic behind any of these calculations, you just have to pay attention to when something crosses the 0 / 360 degree point on the circle. In the case you describe, you can add 360 to any negative values to express them in positive angles. When subtracting angles, if the ending angle is less than the starting angle, you may also need to add 360 to the result to account for crossing the zero point on the unit circle.
Let's try this again:
There are two angles between A and B. One of them is
θ1 = A - B
The other is
θ2 = 360 - θ1
So just take the minimum of those two.
In addition to Brad Larson's excellent answer I would add that you can do:
CGFloat adjustAngle(angle) { return fmod(angle + 180.0, 360.0); }
...
CGFloat difference = fmod(adjustAngle(angle1) - adjustAngle(angle2), 360.0);
Take the difference, add 360, and mod by 360.