Wrong graph when trying to rotate coordinates - math

Consider the following toy data:
clear
input double x1 float y1
0 0
.0013440860215053765 .02503477
.0013440860215053765 .05006954
.005376344086021506 .0751043
.009408602150537635 .10013908
.01747311827956989 .12482615
.03225806451612903 .1498609
.056451612903225805 .1748957
.07661290322580645 .19993046
.09946236559139784 .22496523
.15725806451612903 .25
.2110215053763441 .2750348
.32661290322580644 .3000695
.3803763440860215 .3251043
.4986559139784946 .3497914
.603494623655914 .3748261
.706989247311828 .3998609
.7661290322580645 .4248957
.8064516129032258 .4499305
.885752688172043 .4749652
.9099462365591398 .5
1 .5250348
.9811827956989247 .5500696
.8870967741935484 .5751043
.7661290322580645 .5997913
.6599462365591398 .6248261
.5873655913978495 .6498609
.5282258064516129 .6748957
.40053763440860213 .6999304
.3279569892473118 .7249652
.2163978494623656 .75
.15053763440860216 .7750348
.09408602150537634 .8000696
.06586021505376344 .8247566
.04973118279569892 .8497913
.024193548387096774 .8748261
.025537634408602152 .8998609
.006720430107526882 .9248957
.002688172043010753 .9499304
.004032258064516129 .9749652
0 1
end
twoway scatter y1 x1
When I try to rotate the entire graph by say 20 degrees counter-clockwise:
local theta = 0.349066
generate x2 = (x1 * cos(`theta') ) - (y1 * sin(`theta') )
generate y2 = (x1 * sin(`theta') ) - (y1 * cos(`theta') )
The coordinates transform as follows:
clear
input float(x2 y2)
0 0
-.007299372 -.023065284
-.01586177 -.04659027
-.020635087 -.06873614
-.025408404 -.09088202
-.026273714 -.11132205
-.02094281 -.12979028
-.006770712 -.14504059
.0036123034 -.16167
.016521374 -.17738
.06226916 -.1811377
.10422786 -.1862745
.20428585 -.1702649
.24624455 -.1754017
.3489475 -.1581459
.4389013 -.14581393
.527592 -.13394167
.57460284 -.13723963
.6039312 -.1469735
.6698875 -.1433759
.6840596 -.1586262
.76012 -.151351
.7338752 -.18131188
.6369009 -.23701614
.51478493 -.3015878
.4064434 -.3614295
.3296775 -.4097785
.26554185 -.45353055
.13699183 -.52072746
.06022594 -.5690765
-.05316776 -.630757
-.12361852 -.6768075
-.1852281 -.7196401
-.22019514 -.7524921
-.24391386 -.7815335
-.2764738 -.8137929
-.28377315 -.8368582
-.3100179 -.8668191
-.3223694 -.8917232
-.3296688 -.9147884
-.3420203 -.9396926
end
twoway scatter y2 x2
What am I missing?
Note that I have also tried to center the values first around a specific point.
In addition, I would also like the solution to account for different axes scales and graph aspect ratio.
For example:
clear
input float y double x
-2013 .001
-1941 .0010053763440860215
-1869 .0010053763440860215
-1797 .0010215053763440861
-1725 .0010376344086021505
-1654 .0010698924731182796
-1582 .0011290322580645162
-1510 .0012258064516129032
-1438 .0013064516129032257
-1366 .0013978494623655914
-1294 .0016290322580645162
-1222 .0018440860215053765
-1150 .0023064516129032257
-1078 .0025215053763440864
-1007 .0029946236559139786
-935 .003413978494623656
-863 .003827956989247312
-791 .004064516129032258
-719 .004225806451612904
-647 .004543010752688172
-575 .004639784946236559
-503 .005
-431 .0049247311827956995
-359 .004548387096774194
-288 .004064516129032258
-216 .0036397849462365592
-144 .003349462365591398
-72 .0031129032258064514
0 .0026021505376344085
72 .002311827956989247
144 .0018655913978494624
216 .0016021505376344087
288 .0013763440860215053
359 .0012634408602150537
431 .0011989247311827958
503 .0010967741935483872
575 .0011021505376344087
647 .0010268817204301076
719 .001010752688172043
791 .0010161290322580644
863 .001
end
twoway scatter y x
The y-xis of this graph is 4 inches while the x-axis is 5.5 inches (aspect ratio of 1.375).
I have consulted a number of posts including the following:
Programmatically rotate shapes using coordinates
How to rotate coordinate system?
I hope what I am trying to do is clear but I will be happy to clarify further.

The formulas for rotation are the following:
generate x2 = (x1 * cos(`theta') ) - (y1 * sin(`theta') )
generate y2 = (x1 * sin(`theta') ) + (y1 * cos(`theta') )
These perform rotation about point (0,0).
To make rotation about specific center point (cx, cy), one can apply the next approach:
generate x2 = cx + ((x1 - cx) * cos(`theta') ) - ((y1 - cy) * sin(`theta') )
generate y2 = cy + ((x1 - cx) * sin(`theta') ) + ((y1 - cy) * cos(`theta') )
The above formulas represent affine transformation matrix. To account for axes scale you have to multiply result matrix by scaling matrix - it is very simple, just multiply x or y by coefficient according to axis/axis ratio.
But seems you want to rotate already stretched visual representation. Say your plot is stretched along OX in 5 times. In this case at first multiply internal data x-coordinates by 5, make rotation (note - scale rotation center too), then divide by 5.
For your first example the x-axis is ~1.5 times longer. So we can multiply x-column by 1.5, rotate by 20 degrees, and divide by 1.5. If axes preserve their length, we should see the same plot rotated by 20 degrees. However, data ranges have changed, and plot is resized! Angle is not 20 degrees exactly. This effect will be more evident on the second example with huge axes scale difference.
If ranges remain the same after rotation, I expect that described approach should give correct angles. It might be checked with simple pixel plotting, but I'm afraid this simulation won't reproduce behavior of your plotting system. With automatic axes ranges, exact angles are not possible.

Related

Minimum distance between two circles along a specified vector on a cartesian plane

I am trying to solve the following problem (I am using Matlab, though pseudo-code / solutions in other languages are welcome):
I have two circles on a Cartesian plane defined by their centroids (p1, p2) and their radii (r1, r2). circle 1 (c1 = [p1 r1]) is considered 'dynamic': it is being translated along the vector V = [0 -1]. circle 2 (c2 = [p2 r2]) is considered 'static': it lies in the path of c1 but the x component of its centroid is offset from the x component of c2 (otherwise the solution would be trivial: the distance between the circle centroids minus the sum of their radii).
I am trying to locate the distance (d) along V at which circle 1 will 'collide' with circle 2 (see the linked image). I am sure that I can solve this iteratively (i.e. translate c1 to the bounding box of c2 then converge / test for intersection). However, I would like to know if there is a closed form solution to this problem.
Shift coordinates to simplify expressions
px = p1.x - p2.x
py = p1.y - p2.y
And solve quadratic equation for d (zero, one, or two solutions)
px^2 + (py - d)^2 = (r1 + r2)^2
(py - d)^2 = (r1 + r2)^2 - px^2
d = py +/- Sqrt((r1 + r2)^2 - px^2)
That's all.
As the question title does not match the question and accepted answer which is dependent on a fixed vector {0, -1}, or {0, 1} rather than an arbitrary vector I have added another solution that works for any unit vector.
Where (See diagram 1)
dx, dy is the unit vector of travel for circle c1
p1, p2 the centers of the moving circle c1 and static circle c2
r1, r2 the radius of each circle
The following will set d to the distance c1 must travel along dx, dy to collide with c2 if no collision the d will be set to Infinity
There are three cases when there is no solution
The moving circle is moving away from the static circle. u < 0
The moving circle never gets close enough to collide. dSq > rSq
The two circles are already overlapping. u < 0 luckily the math makes
this the same condition as moving away.
Note that if you ignore the sign of u (1 and 3) then d will be the distance to first (causal) contact going backward in time
Thus the pseudo code to find d
d = Infinity
rSq = (r1 + r2) ^ 2
u = (p1.x - p2.x) * dx + (p1.x - p2.x) * dy
if u >= 0
dSq = ((p2.x + dx * u) - p1.x) ^ 2 + ((p2.y + dy * u) - p1.y) ^ 2
if dSq <= rSq
d = u - (rSq - dSq) ^ 0.5
The point of contact can be found with
cpx = p1.x + dx * d;
cpy = p1.x + dy * d;
Diagram 1

Getting direction vectors from 3d position and/or rotation

I have a camera, in which, I've obtained its 3d position and its 3d rotation (in degrees). The coordinate system is as follows:
The order is XYZ and is intrinsic.
pitch
roll
yaw
(aka right/front/top vector)
With this information, how can I calculate the Up and Front vectors?
Yaw rotation is about OY (vertical) axis and has matrix MY (for CCW positive)
Cos(Yaw) -Sin(Yaw) 0
Sin(Yaw) Cos(Yaw) 0
0 0 1
Pitch rotation is about OZ axis and has matrix MP
Cos(Pitch) 0 Sin(Pitch)
0 1 0
-Sin(Pitch) 0 Cos(Yaw)
Roll rotation is about OX axis and has matrix MR
1 0 0
0 Cos(Roll) -Sin(Roll)
0 Sin(Roll) Cos(Roll)
Now one has to multiply matrices MP, MR, MY in right order and get matrix M.
To find forward direction vector - multiply M and vector [1, 0, 0]. To get top direction vector - multiply M and vector [0, 1, 0]. To get side direction vector - multiply M and vector [1, 0, 0].
I cannot check right matrix multiplication order and the last result now. If you have any symbolic math software like Maple, Matlab - try it.
Result should look like this for forward
X = Cos(Pitch) * Cos(Yaw)
Y = Sin(Pitch)
Z = Cos(Pitch) * Sin(Yaw)
and up (perhaps wrong)
X = -Cos(Yaw) * Sin(Roll) - Sin(Yaw) * Sin(Pitch) * Cos(Roll)
Y = Sin(Yaw) * Sin(Roll) - Cos(Yaw) * Sin(Pitch) * Cos(Roll)
Z = Cos(Pitch) * Cos(Roll)

Finding the coordinates of a point contained by a trapezoid programmatically

If I have a trapezoid defined by four points (x1, y1), (x2, y2), (x3, y3), (x4, y4) (chosen as (255, 0), (255, 235), (200, 35), and (200, 235) for the sake of the example), and I divide it arbitrarily in n by m sections like so (pardon the crude drawing):
How could I find the coordinates of (x, y)?
I've tried fooling around with the slopes of the lines, but my math skills are too rusty to figure it out. Any ideas?
For the specific case as per your example it's quite straight forward.
First, the x location is easy. Since the verticals will always be parallel to the y axis, x is simply x width divided by the number of sections:
x = x1+((x2-x1)/a*xa)
where:
x = result coordinate
x1,x2 = edges of the trapezoid
a = number of sections
xa = x coordinate in term of sections
note: I chose a to avoid confusion with the symbol for slope: m.
For y it's a bit more complicated. We first need to find the coordinate that sits on the top and bottom lines of the trapezoid. We use the standard line equation for this:
y = mx+c
Finding m is simple. It's just Dy/Dx:
m = (y2-y1)/(x2-x1)
To get c we just substitute x, y and m into the formula:
c = y-mx
Once we get that, substitute the value of x found earlier into the formula to get the y coordinate. Do this twice to get the points on the top and bottom lines:
1 A 2
x------------x--------------x
| | |
| xC |
| | |
x------------x--------------x
3 B 4
All together (pseudocode):
coordinateFromSection (x1 y1 x2 y2 x3 y3 x4 y4 gridX gridY sectionX sectionY) {
xC = x1+((x2-x1)/gridX*sectionX)
// top line:
m = (y2-y1)/(x2-x1)
c = y1-(m*x1)
yA = m*xC + c
// bottom line:
m = (y4-y3)/(x4-x3)
c = y3-(m*x3)
yB = m*xC + c
// Find yC by dividing line AB by gridY
yC = yA+((yB-yA)/gridY*sectionY)
return (xC yC)
}
All the calculations above assume that (0,0) is the top left of the screen.

Drawing rectangle between two points with arbitrary width

I'm trying to draw a line between two (2D) points when the user swipes their finger across a touch screen. To do this, I plan on drawing a rectangle on every touch update between the X and Y of the previous touch update and the X and Y of the latest touch update. This should create a continuous and solid line as the user swipes their finger across the screen. However, I would also like this line to have an arbitrary width. My question is, how should I go about calculating the coordinates I need for each rectangle (x1, y1, x2, y2)?
--
Also: if anyone has any information on how I could then go about applying anti-aliasing to this line it'd be a massive help.
Calculate a vector between start and end points
V.X := Point2.X - Point1.X;
V.Y := Point2.Y - Point1.Y;
Then calculate a perpendicular to it (just swap X and Y coordinates)
P.X := V.Y; //Use separate variable otherwise you overwrite X coordinate here
P.Y := -V.X; //Flip the sign of either the X or Y (edit by adam.wulf)
Normalize that perpendicular
Length = sqrt(P.X * P.X + P.Y * P.Y); //Thats length of perpendicular
N.X = P.X / Length;
N.Y = P.Y / Length; //Now N is normalized perpendicular
Calculate 4 points that form a rectangle by adding normalized perpendicular and multiplying it by half of the desired width
R1.X := Point1.X + N.X * Width / 2;
R1.Y := Point1.Y + N.Y * Width / 2;
R2.X := Point1.X - N.X * Width / 2;
R2.Y := Point1.Y - N.Y * Width / 2;
R3.X := Point2.X + N.X * Width / 2;
R3.Y := Point2.Y + N.Y * Width / 2;
R4.X := Point2.X - N.X * Width / 2;
R4.Y := Point2.Y - N.Y * Width / 2;
Draw rectangle using these 4 points.
Here's the picture:
EDIT: To answer the comments: If X and Y are the same then the line is exactly diagonal and perpendicular to a diagonal is a diagonal. Normalization is a method of making a length to equal to 1, so that the width of your line in this example will not depend on perpendiculars length (which is equal to lines length here).
Easy way (I'll call the "width" the thickness of the line):
We need to calculate 2 values, the shift on the x axis and the shift on the y axis for each of the 4 corners. Which is easy enough.
The dimensions of the line are:
width = x2 - x1
height = y2 - y1
Now the x shift (let's call it xS):
xS = (thickness * height / length of line) / 2
yS = (thickness * width / length of line) / 2
To find the length of the line, use Pythagoras's theorem:
length = square_root(width * width + height * height)
Now you have the x shift and y shift.
First coordinate is: (x1 - xS, y1 + yS)
Second: (x1 + xS, y1 - yS)
Third: (x2 + xS, y2 - yS)
Fourth: (x2 - xS, y2 + yS)
And there you go! (Those coordinates are drawn counterclockwise, the default for OpenGL)
If I understand you correctly, you have two end points say A(x1,y1) and B(x2,y2) and an arbitrary width for the rectangle say w. I assume the end points will be just at the middle of the rectangle's shorter sides meaning the distance of the final rectangles corner coordinates would be w/2 to A and B.
You can compute the slope of the line by;
s1 = (y2 - y1) / (x2 - x1) // assuming x1 != x2
The slope of the shorter sides is nothing but s2 = -1/s1.
We have slope, we have distance and we have the reference points.
We than can derive two equations for each corner point:
For one corner closer to A
C(x3,y3):
(y3 - y1) / (x3 - x1) = s2 // by slope
(y3 - y1)^2 + (x3 - x1)^2 = (w/2)^2 // by distance
replacing (y3 - y1) by a and (x3 - x1) by b yields
a = b * s2 // slope equation
// replace a by b*s2
b^2 * s2^2 + b^2 = (w/2)^2 // distance equaiton
b^2 = (w/2)^2 / (s2^2+1)
b = sqrt((w/2)^2 / (s2^2+1))
we know w and s2 and hence compute b
If b is known, we can deduce x3
x3 = b + x1
and a, as well
a = b * s2
and so y3
y3 = b*s2 + y1
we have one corner point C(x3,y3).
To compute the other corner point closer to A, say D(x4,y4), the slope equation can be constructed as
(y1 - y4) / (x1 - x4) = s2
and the calculations listed above should be applied.
Other two corners can be calculated with the steps listed here replacing A(x1, y1) with B(x2,y2).

Computing the 3D coordinates on a unit sphere from a 2D point

I have a square bitmap of a circle and I want to compute the normals of all the pixels in that circle as if it were a sphere of radius 1:
The sphere/circle is centered in the bitmap.
What is the equation for this?
Don't know much about how people program 3D stuff, so I'll just give the pure math and hope it's useful.
Sphere of radius 1, centered on origin, is the set of points satisfying:
x2 + y2 + z2 = 1
We want the 3D coordinates of a point on the sphere where x and y are known. So, just solve for z:
z = Âħsqrt(1 - x2 - y2).
Now, let us consider a unit vector pointing outward from the sphere. It's a unit sphere, so we can just use the vector from the origin to (x, y, z), which is, of course, <x, y, z>.
Now we want the equation of a plane tangent to the sphere at (x, y, z), but this will be using its own x, y, and z variables, so instead I'll make it tangent to the sphere at (x0, y0, z0). This is simply:
x0x + y0y + z0z = 1
Hope this helps.
(OP):
you mean something like:
const int R = 31, SZ = power_of_two(R*2);
std::vector<vec4_t> p;
for(int y=0; y<SZ; y++) {
for(int x=0; x<SZ; x++) {
const float rx = (float)(x-R)/R, ry = (float)(y-R)/R;
if(rx*rx+ry*ry > 1) { // outside sphere
p.push_back(vec4_t(0,0,0,0));
} else {
vec3_t normal(rx,sqrt(1.-rx*rx-ry*ry),ry);
p.push_back(vec4_t(normal,1));
}
}
}
It does make a nice spherical shading-like shading if I treat the normals as colours and blit it; is it right?
(TZ)
Sorry, I'm not familiar with those aspects of C++. Haven't used the language very much, nor recently.
This formula is often used for "fake-envmapping" effect.
double x = 2.0 * pixel_x / bitmap_size - 1.0;
double y = 2.0 * pixel_y / bitmap_size - 1.0;
double r2 = x*x + y*y;
if (r2 < 1)
{
// Inside the circle
double z = sqrt(1 - r2);
.. here the normal is (x, y, z) ...
}
Obviously you're limited to assuming all the points are on one half of the sphere or similar, because of the missing dimension. Past that, it's pretty simple.
The middle of the circle has a normal facing precisely in or out, perpendicular to the plane the circle is drawn on.
Each point on the edge of the circle is facing away from the middle, and thus you can calculate the normal for that.
For any point between the middle and the edge, you use the distance from the middle, and some simple trig (which eludes me at the moment). A lerp is roughly accurate at some points, but not quite what you need, since it's a curve. Simple curve though, and you know the beginning and end values, so figuring them out should only take a simple equation.
I think I get what you're trying to do: generate a grid of depth data for an image. Sort of like ray-tracing a sphere.
In that case, you want a Ray-Sphere Intersection test:
http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter1.htm
Your rays will be simple perpendicular rays, based off your U/V coordinates (times two, since your sphere has a diameter of 2). This will give you the front-facing points on the sphere.
From there, calculate normals as below (point - origin, the radius is already 1 unit).
Ripped off from the link above:
You have to combine two equations:
Ray: R(t) = R0 + t * Rd , t > 0 with R0 = [X0, Y0, Z0] and Rd = [Xd, Yd, Zd]
Sphere: S = the set of points[xs, ys, zs], where (xs - xc)2 + (ys - yc)2 + (zs - zc)2 = Sr2
To do this, calculate your ray (x * pixel / width, y * pixel / width, z: 1), then:
A = Xd^2 + Yd^2 + Zd^2
B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2
Plug into quadratic equation:
t0, t1 = (- B + (B^2 - 4*C)^1/2) / 2
Check discriminant (B^2 - 4*C), and if real root, the intersection is:
Ri = [xi, yi, zi] = [x0 + xd * ti , y0 + yd * ti, z0 + zd * ti]
And the surface normal is:
SN = [(xi - xc)/Sr, (yi - yc)/Sr, (zi - zc)/Sr]
Boiling it all down:
So, since we're talking unit values, and rays that point straight at Z (no x or y component), we can boil down these equations greatly:
Ray:
X0 = 2 * pixelX / width
Y0 = 2 * pixelY / height
Z0 = 0
Xd = 0
Yd = 0
Zd = 1
Sphere:
Xc = 1
Yc = 1
Zc = 1
Factors:
A = 1 (unit ray)
B
= 2 * (0 + 0 + (0 - 1))
= -2 (no x/y component)
C
= (X0 - 1) ^ 2 + (Y0 - 1) ^ 2 + (0 - 1) ^ 2 - 1
= (X0 - 1) ^ 2 + (Y0 - 1) ^ 2
Discriminant
= (-2) ^ 2 - 4 * 1 * C
= 4 - 4 * C
From here:
If discriminant < 0:
Z = ?, Normal = ?
Else:
t = (2 + (discriminant) ^ 1 / 2) / 2
If t < 0 (hopefully never or always the case)
t = -t
Then:
Z: t
Nx: Xi - 1
Ny: Yi - 1
Nz: t - 1
Boiled farther still:
Intuitively it looks like C (X^2 + Y^2) and the square-root are the most prominent figures here. If I had a better recollection of my math (in particular, transformations on exponents of sums), then I'd bet I could derive this down to what Tom Zych gave you. Since I can't, I'll just leave it as above.

Resources