How does scaling this sin wave work? - scaling

So I have to scale sin from
-1 to 1
to
0 to 255.
I added one to sin then multiplied it by 255/2.
It works, but isn't this is a completely different point on the graph because of the addition of one?

Related

Graph Visualization Represented in a Circle: How To

Graph Visualization represented in a Circle: How To
I am trying to represent a plotted graph line around a circle.
Where the center is 0
Intervals of 45 degrees / 8 values.
Greatest value = 1 / outer boundary of circle.
I want to plot the graph at each interval
Points at right angles are straight forward
I could hack this pretty easy but I'd rather know the math in case I ever want to do more complex things.
I am looking for the math to figure out where the 45 degree increments should be. For example: if the point is .33 of 1 then how do I know where it will be at 45 degrees or at 13 degrees, etc. etc.
Why Lua?
I'm coding in lua so that would be the perferable
EDIT: Made a picture but I don't have enough rep :(
Bar 1 # 0 Deg = Lenght of 1 = x,y of 0,1
Bar 2 # 45 Deg = Lenght of .33 = x,y of ?,?
Bar 3 # 90 Deg = Lenght of .5 = x,y of .5,0
Bar 4 # 105 Deg = Lenght of .66 = x,y of ?,?
How do I get the x,y of Bar 2 and Bar 4?
The easiest way is with polar coordinates where:
x = r cos φ and y = r sin φ
(r would be your length and φ would be your angle)
The one wrinkle is that in polar coordinates, φ = 0 is along the positive x-axis and increasing angles rotate counter-clockwise. To account for the offset in 0° we just subtract 90° from your desired angle. Then to change the rotation to clockwise, we just take the negative of the result. So,
phi = -(angle - 90)
x = length * cos(phi)
y = length * sin(phi)
For your current problem with only 8 angles you could calculate these by hand pretty readily knowing that both cos and sin of 45° is about 0.707.

How to transform a co-ordinate value in 45 deg-135 deg co-ordinate system to earth co-ordinate system?

I get a series of square binary images as in the picture below,
I want to find the red point, which is the point of intersection of four blocks (2 black and 2 white). For doing so, I use to get the sum of all pixel values along the diagonal directions of the square image, which is 45 deg and 135 deg respectively. The intersection of maximum pixel sum 45 deg line and minimum pixel sum 135 deg line is where my red point is.
Now that I get the co-ordinate of the red point in 45 deg-135 deg co-ordinate system, how to I transform them to earth co-ordinates?
In other words, say I have a point in 45deg-135deg co-ordinate system; How do I find the corresponding co-ordinate values in x-y co-ordinate system? What is the transformation matrix?
some more information that might help:
1) if the image is a 60x60 image, I get 120 values in 45deg-135deg system, since i scan each row followed by column to add the pixels.
I don't know much about matlab, but in general all you need to do is rotate your grid by 45 degrees.
Here's a helpful link; shows you the rotation matrix you need
wikipedia rotation matrix article
The new coordinates for a point after 2D rotation look like this:
x' = x \cos \theta - y \sin \theta.
y' = x \sin \theta + y \cos \theta.
replace theta with 45 (or maybe -45) and you should be all set.
If your red dot starts out at (x,y), then after the -45 degree rotation it will have the new coordinates (x',y'), which are defined as follows:
x' = x cos(-45) - y sin (-45)
y' = x sin (-45) + y cos (-45)
Sorry when I misunderstood your question but why do you rotate the image? The x-value of your red point is just the point where the derivative in x-direction has the maximum absolute value. And for the y-direction it is the same with the derivative in y-direction.
Assume you have the following image
If you take the first row of the image it has at the beginning all 1 and the for most of the width zeroes. The plot of the first column looks like this.
Now you convolve this line with the kernel {-1,1} which is only one nested loop over your line and you get
Going now through this result and extracting the position of the point with the highest value gets you 72. Therefore the x-position of the red point is 73 (since the kernel of the convolution finds the derivative one point too soon).
Therefore, if data is the image matrix of the above binary image then extracting your red point position is near to one line in Mathematica
Last[Transpose[Position[ListConvolve[{-1, 1}, #] & /#
{data[[1]],Transpose[data][[1]]}, 1 | -1]]] + 1
Here you get {73, 86} which is the correct position if y=0 is the top row. This method should be implemented in a few minutes in any language.
Remarks:
The approximated derivative which is the result of the convolution can either be negative or positive. This depends whether it is a change from 0 to 1 or vice versa. If you want to search for the highest value, you have to take the absolute value of the convolution result.
Remember that the first row in the image matrix is not always in top position of the displayed image. This depends on the software you are using. If you get wrong y values be aware of that.

Finding if an angle lies between two points

This is basically just a math question.
Heres what I am having troubles with... I am having a difficult time coming up with how to phrase the question, so bear with me. Basically I think I need to use some advanced math to accomplish this, but I do not know what I need.
I will use some illustrations to make this clear. Spam prevention doesn't let me post pictures... Here's a simple concept image though: http://radleygh.com/images/gimp-2_2011-057-00-57-26-40.bmp
Objective: Determine if several objects lie within a cone on a 2D plane
Cone Properties:
Position (x, y)
Angle (0-359)
Spread (0-359, aka Width)
Distance (0++)
I can decide the brownish lines using a simple bit of math:
Angle_A = Angle + (Spread / 2)
Angle_B = Angle - (Spread / 2)
Angle_Target = Point_Direction(origin, object_position)
Now I thought of comparing these with the position of each object with a simple if/then statement:
If (Angle_A > Angle_Target) && (Angle_B < Angle_Target) Then Angle_Target is between A and B
This works... untill Angle_A or Angle_B pass the 0-360 threshold. 0* is between 45* and 315*... but the above if statement wouldn't work. We can then determine which direction to check based on the size of the cone...
And what if the cone effect is larger than a 180* cone?
I'm not sure of the answer. I'm pretty sure I should be using Radians... But I do not understand the concept of Radians. if someone can point me in the right direction, perhaps show me an example somewhere, that would be wonderful!
I will continue to do my own research in the mean time.
You may consider a simple transformation which sets your coordinate system such that Angle_B is zero. In other words, instead of testing
Angle_B < Angle_Target < Angle_A
you may also use
0 < Angle_Target - Angle_B < Angle_A - Angle_B
If you apply a modulo 360° to all terms you're logic should work:
0 < (Angle_Target - Angle_B) % 360 < (Angle_A - Angle_B) % 360
One radian is the angle made by tracing a circle's circumference by a length equal to that circle's radius. Hence there are exactly 2*PI radians in a circle.
So 2*PI radians = 360 degrees
So to convert degrees to radians, multiply by 2 * PI, then divide by 360. (Or of course, multiply by PI, divide by 180).
However, whether you work in radians or degrees should only be dictated by the library you are using. Even then, you could write wrappers which do the above calculations.
But to the main part of your question. Consider that:
sin (theta) = sin (360 + theta).
cos (theta) = cos (360 + theta).
etc.
So if you come across your cone that goes through 0 degrees, simply add 360 to both angles of the cone.
e.g. if your cone goes from -10 to +20, simply use 350 to 380 instead.
And of course, when you test an angle, make sure you also add 360 to that and test both the original and added angles.
e.g. testing +5 (which is in your cone), you would test 5 (which fails) then 365 (which passes).
Good luck!

rotating a 2d square into another

I have two squares, S1 = (x1,y1,x2,y2) and S2 = (a1,b1,a2,b2)
I'm looking for the A transformation matrix with which
A * S1 = S2
As far as I see, A is an affine 3x3 matrix, so I have 9 unknown values.
How can I calculate these values?
thanks and best,
Viktor
There are really only four unknown values here. A rotation angle, a scale factor and an x and y translation. Of your three by three matrix the bottom row is always 0,0,1 which reduces you to six unknowns. The right hand column will be Tx,Ty,1 which are your translations (and the 1 we already know about).
The two by two "matrix" left will be your rotation and scaling. This will (off the top of my head) be something like:
ACos(B), -Asin(B)
ASin(B), aCos(B)
So in total:
ACos(B), -Asin(B), Tx
ASin(B), ACos(B), Ty
0 , 0 , 1
You extend your co-ordinate matrices with the 1 on the end of each co-ordinate to give 2x3 matrices and they then multiply to give you the four equations you need to solve for the four variables. That is left as an exercise for the reader.
A transformation matrix is a factor of scaling matrix Ss, transition matrix St and rotation matrix Sr.
Assume the old point is Po is (Xo,Yo) and as vector will be represented as (Xo Yo 1)' same for the new point Pn
Then Pnv =SsStSrPov
Where Sx is
Sx 0 0
0 Sy 0
0 0 1
St is
1 0 Tx
0 1 Ty
0 0 1
Sr is
Cos(th) -Sin(th) 0
Sin(th) Cos(th) 0
0 0 1
Now back to your question. if two point are giving to represent a rectangle we can just find the parameter of two matrix and the third one will be an identity matrix.
Rect1 is represented as Top-Left point P11 and Bottom-Right Point P12
Rect2 is represented as Top-Left point P21 and Bottom-Right Point P22
S=Ss*St
Sx 0 Tx
0 Sy Ty
0 0 1
Now you have 4 missing parameters and 4 set of equations
P21=S*P11
P22=S*P12
X[P21] =Sx*X[P11]+Tx
Y[P21] =Sy*Y[P11]+Ty
X[P22] =Sx*X[P12]+Tx
Y[P22] =Sy*Y[P12]+Ty
Solve it and you'll get your answer.
and if you have transition and rotation then
S=Sr*St.
Cos(th) -Sin(th) Tx
Sin(th) Cos(th) Ty
0 0 1
Now you have 3 missing parameters and 4 set of equations
P21=S*P11
P22=S*P12
X[P21] =Cos(th)*X[P11]-Sin(th)*Y[P11]+Tx
Y[P21] =Sin(th)*X[P11]+Cos(th)*Y[P11]+Ty
X[P22] =Cos(th)*X[P11]-Sin(th)*Y[P12]+Tx
Y[P22] =Sin(th)*X[P11]+Cos(th)*Y[P12]+Ty
Replace Cos(th) with A and Sin(th) With B and solve the equations.
X[P21] =A*X[P11]-B*Y[P11]+Tx
Y[P21] =B*X[P11]+A*Y[P11]+Ty
X[P22] =A*X[P11]-B*Y[P12]+Tx
Y[P22] =B*X[P11]+A*Y[P12]+Ty
Check if its correct A^2+B^2 =? 1 if is true then th = aCos(A)
The last part of the solution, if you'll have all three matrixes, then S=SrStSs is
Sx*sin(th) -Sx*cos(th) Tx
Sy*cos(th) Sy*sin(th) Ty
0 0 1
Now we have 5 missing variables and we need 6 different set of equations to solve it. which is mean 3 points from each rectangle.
You shouldn't have a 3x3 matrix if you're just looking to transform a 2D object. What you're looking for is a 2x2 matrix that solves A*S1=S2. This can be done in many different ways; in MATLAB, you'd do a S2/S1 (right matrix division), and generally this performs some kind of Gaussian elimination.
How can I calculate these values?
When applied to 2d/3d transformations, matrix can be represented a coordinate system, unless we are talking about projections.
Matrix rows (or columns, depending on notation) form axes of a new coordinate system, in which object will be placed placed if every object vertex is multiplied by the matrix. Last row (or columne, depending on notation) points to the center of the new coordinate system.
Standard OpenGL/DirectX transformation matrix (NOT a projection matrix):
class Matrix{//C++ code
public:
union{
float f[16];
float m[4][4];
};
};
Can be represented as combination of 4 vectors vx (x axis of the new coordinate system), vy(y axis of a new coordinate system), vz(z axis of a new coordinate system), and vp (center of the new system). Like this:
vx.x vx.y vx.z 0
vy.x vy.y vy.z 0
vz.x vz.y vz.z 0
vp.x vp.y vp.z 1
All "calculate rotation matrix", "calculate scale matrix", etc go down to this idea.
Thus, for 2d matrix, you'll have 3x3 matrix that consists of 3 vectors - vx, vy, vp, because there is no z vector in 2d. I.e.:
vx.x vx.y 0
vy.x vy.y 0
vp.x vp.y 1
To find a transform that would transform quad A into quad B, you need to find two transforms:
Transform that will move quad A into origin (i.e. at point zero), and convert it into quad of fixed size. Say, quad (rectangle) whose one vertex x = 0, y = 0, and whose vertices are located at (0, 1), (1, 0), (1, 1).
Transform that turns quad of fixed size into quad B.
You CANNOT do that it this way if opposite edges of quad are not parallel. I.e. parallelograms are fine, but random 4-sided polygons are not.
A quad can be represented by base point (vp) which can be any vertex of the quad and two vectors that define quad sizes (direction of the edge multiplied by edge's length). I.e. "up" vector and "side" vector. Which makes it a matrix:
side.x side.y 0
up.x up.y 0
vp.x vp.y 1
So, multiplying a quad (vp.x = 0, vp.y = 0, side.x = 1, side.y = 0, up.x = 0, up.y = 1) by this matrix will turn original quad into your quad. Which means, that in order to transform
quad A into quad B, you need to do this:
1) make a matrix that would transform "base 1unit quad" into quad A. Let's call it matA.
2) make a matrix that would transform "base 1 unit quad" into quad B. let's call it matB.
3) invert matA and store result into invMatA.
4) the result matrix is invMatA * matB.
Done. If you multiply quad A by result matrix, you'll get quad B. This won't work if quads have zero widths or heights, and it won't work if quads are not parallelograms.
This is hard to understand, but I cannot to make it simpler.
What do you mean by S1 = (x1,y1,x2,y2)?
Do they represent the top-left and bottom-right corners of the square?
Also, can you guarantee there's only rotation between the squares or do you need a full affine transformation which allows for scaling, skewing, and translation?
Or do you also need a perspective transformation?
Only if it's a perspective transformation, will you need 3x3 matrix with 8 dof as you've mentioned in your post.

How do I break a moving objects speed up into its X and Y velocity given a fixed speed/angle?

Math escapes me today.
How do I find the X speed and the Y speed of an object if it is going at a defined speed (say, 5 pixels/second) at a 45 degree angle?
So always 5 pixels/sec and always 45 degrees?
The general case is
velx=cos(a)*vel;
vely=sin(a)*vel;
a is angle, usually in radians, so convert from degrees, and the signs (positive/negative) will depend on your coordinate system.
Crazy fact from the 1980s: In the old days, we used lookup tables for sin and cos!
Edited: Made my axes more conventional thanks to comment below. x is positive to your right. y is positive up. 45 degrees is to the northeast. If you have something else, let me know.
It will be
Vx=VCos#
Vy=Vsin#
So in your case it will be Vx=5*cos45 and Vy=5*sin45
At 45 angle value of Cos & Sin is same i.e 1/root 2.
Note: If you are doing any math stuff in programming then have a look at Vecmath lib.
At a 45 degree angle, an object is going sqrt(2)/2 of the speed along each axis. Generally, you can do it with sin and cosine, but for specific angles like this you can do it just by knowing pythagorean triangles.
In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. You know the hypotenuse is V. You also know that the other two sides equal each other. That means that V^2 = Vx^2 * 2. This means that Vx = sqrt(V^2/2), which equals V * sqrt(1/2).

Resources