Rotation around the origin - c#

I'm trying to rotate a point around the origin but i'm not sure what exactly i'm doing wrong, i'm using the built in System.Windows.Media.Media3D Namespace in PresentationCore:
var id = Matrix3D.Identity;
id.Rotate(new Quaternion(new Vector3D(1,0,0),90)); // Rotate around the X axis 90 degrees
var pt = new Point3D(0,0,10);
var p2 = id.Transform(pt); // Expect point to be rotated around the X axis 90 degree
Expected value of p2 is
x:0;
y:10;
z:0;
Actual value is
x:0;
y:-10;
z:2,22044604925031E-15
I'm sure i'm making a really basic error but i can't spot it.

(This answer assumes you are using and is using european notation of comma (,) instead of point (.))
You are getting the correct answer. 2,22044604925031 * 10^-15 is such a small number it is practically zero, but with round off error.
It is equal to about: 0,0000000000000022, close enough to 0 for most practical purposes.

Related

Input a geographic coordinate, return a coordinate within x miles of that inputted coordinate - C# .NET

As the title suggests, I am trying to generate a coordinate based on another coordinate that is within an x mile (or whichever unit is most convenient) radius of the inputted one.
As an example:
I am given a geographic coordinate (lat, lon) of 39.083056, -94.820200.
I want to be returned another set of coordinates that is within a x
miles radius of that coordinate, such as 39.110998, -94.799668.
The x mile radius isn't as important as the fact that the returned
coordinates are within that x mile radius.
I have searched and searched, but I must be searching the wrong thing because all the posts that I have been able to find seem like they get very close to what I am trying to do but aren't quite hitting the nail on the head.
I'm sorry you're being downvoted to oblivion. I understand it can be frustrating trying to search for something without knowing what exactly to search for.
You may be interested in Orthodromic Lines/Distances: wiki. If this answer doesn't fulfil your needs, at least you have a new term to google and hopefully will lead you to one that does suit.
You could try using the Geo library. Its documentation is on the sparse side, but it does contain a method that could be useful to you: CalculateOrthodromicLine(startPoint, heading, distance)
A pseudocode would be something as simple as this:
var startPoint = new Coordinate(lat, long);
var heading = Random between 0 and 360 degrees
var distance = Random between 0 and X metres
var endPoint = //<-- et voila!
GeoContext.Current.GeodeticCalculator
.CalculateOrthodromicLine(startPoint, heading, distance)
.Coordinate2;
Edit: As mentioned in the wiki, the Earth is not a perfect sphere, but a spheroid instead. The library's GeoContext.Current by default uses its Spheroid calculations, so you should be okay.
Good luck!

Get Points of Intersection with Sine Wave

I have a particle at a fixed position X and fixed amplitude AMP that travels on a sin wave.
I'd like to be able to dynamically change the period of the target sine wave WITHOUT moving the particle. (When I change the period, its X position doesn't line up to the same Y position of the graph with a different period, so it jumps all over the place.
I have a solution for this - Find where the graph with the new period intersects with the graph of the old period and create an X-offset, lining these two figures up.
For any given y on a sin wave, there will be 2 corresponding X values per period. I'm trying to get these two values. I'll then compare the derivatives to choose which one I want.
However, I don't know how to get both of these values. Any ideas?
Here's an image of what I'm after: Sin Wave Logic
If you have a sine-wave defined by
y = sin(a*(x+c))+d
then you can reverse it by
x = (arcsin(y-d)-c)/a
for a given a.
This yields the solution in the range of [-pi;pi]
The other solution you are looking for is then 2*pi-x
If you allow a little cheating, you can use a cross-fading effect to get a smooth transition from f(t) to g(t) via
h(s,t)=(1-s)*f(t)+s*g(t)
while s as a function of t is between 0 and 1 and use something like
y = h( (t-ts)/d, t)
for the time period from ts to ts+d.

Bouncing a ball off a wall with arbitrary angle?

I'm trying to let the user draw a paddle that they can then use to hit a ball. However, I cannot seem to get the ball to bounce correctly because the x and y components of the ball's velocity are not lined up with the wall. How can I get around this?
I tried the advice given by Gareth Rees here, but apparently I don't know enough about vectors to be able to follow it. For example, I don't know what exactly you store in a vector - I know it's a value with direction, but do you store the 2 points it's between, the slope, the angle?
What I really need is given the angle of the wall and the x and y velocities as the ball hits, to find the new x and y velocities afterwards.
Gareth Rees got the formula correct, but I find the pictures and explanation here a little more clear. That is, the basic formula is:
Vnew = -2*(V dot N)*N + V
where
V = Incoming Velocity Vector
N = The Normal Vector of the wall
Since you're not familiar with vector notation, here's what you need to know for this formula: Vectors are basically just x,y pairs, so V = (v.x, v.y) and N = (n.x, n.y). Planes are best described by the normal to the plane, that is a vector of unit length that is perpendicular to the plane. Then a few formula, b*V = (b*v.x, b*v.y); V dot N = v.x*n.x+v.y*n.y, that is, it's a scalar; and A + B = (a.x+b.x, a.y+b.y). Finally, to find a unit vector based on an arbitrary vector, it's N = M/sqrt(M dot M).
If the surface is curved, use the normal at the point of contact.

projection of point on line in 3D

I have point with x,y,z
and line direction x,y,z
how to get the point projection on this line
I tried this code
http://www.zshare.net/download/93560594d8f74429/
for example when use the function intersection in the code I got
the line direction is (1,0,0) and the point (2,3,3) will have projection (value in x , 0, 0 ) and this is wrong value
any suggestion
Best regards
You want to project the vector (x,y,z) on the line with direction (a,b,c).
If (a,b,c) is a unit vector then the result is just (x,y,z).(a,b,c) (a,b,c) = (ax+by+cz)(a,b,c)
If it's not a unit vector make it one, divising it by its norm.
EDIT : a little bit of theory:
Let E be your vectorial space of dimension N:
let F be the line directed by vector a. The hyperplan orthogonal to F is :
Now let's chose a vector x in E, x can be writen as :
where xF is the coordinate of x in the direction of F, an x orthogonal is the coordinate on the orthogonal hyperplan.
You want to find xF: (it's exactly the same formula as the one I wrote above)
You should have a close look at the wikipedia article on orthogonal projections and try to find more stuff on the web .
You can generalise that to any F, if it's not a line anymore but a plan then take F orthogonal and decompose x the same way...etc.
This topic is clearly old and I think the original poster meant vector not line. But for the purposes of Google:
A line, unlike a vector does not (necessarily) have its origin at (0,0,0). So cannot be described just by a direction, it also needs an origin. This is the zero point of the line; the line can extend beyond and before this point, but when you say you’re zero meters along the line this is where you mean.
So to get the projection of a point onto a line you first need to convert the point into the local co-ordinate frame, which you do by subtracting the origin from the point (e.g. if a fence post is the ‘line’ you go from GPS co-ordinates to ‘5 metres to the north and a meter above the bottom of the fence post‘). Now in this local co-ordinate frame the line is just a vector, so we can get the projection of the point using the normal dot product approach.
pointLocalFrame = point– origin
projection = dotProduct(lineDirection, pointLocalFrame)
NOTE: this assumes the line is infinite in length, if the projection is greater than the actual line length then there is no projection
NOTE: lineDirection must be normalised; i.e. its length must be 1
NB: dot product of two vectors (x1,y1,z1) and (x2,y2,z2) is x1*x2+y1*y2+z1*z2

Distance to a plane

I've written a simple little helper method whoch calculates the distance from a point to a plane. However, it seems to be returning nonsensical results. The code i have for creating a plane is thus:
Plane = new Plane(vertices.First().Position, vertices.Skip(1).First().Position, vertices.Skip(2).First().Position);
Fairly simple, I hope you'll agree. It creates an XNA plane structure using three points.
Now, immediately after this I do:
foreach (var v in vertices)
{
float d = Math.Abs(v.ComputeDistance(Plane));
if (d > Constants.TOLERANCE)
throw new ArgumentException("all points in a polygon must share a common plane");
}
Using the same set of vertices I used to construct the plane, I get that exception thrown! Mathematically this is impossible, since those three points must lie on the plane.
My ComputeDistance method is:
public static float ComputeDistance(this Vector3 point, Plane plane)
{
float dot = Vector3.Dot(plane.Normal, point);
float value = dot - plane.D;
return value;
}
AsI understand it, this is correct. So what could I be doing wrong? Or might I be encountering a bug in the implementation of XNA?
Some example data:
Points:
{X:0 Y:-0.5000001 Z:0.8660254}
{X:0.75 Y:-0.5000001 Z:-0.4330128}
{X:-0.75 Y:-0.5000001 Z:-0.4330126}
Plane created:
{Normal:{X:0 Y:0.9999999 Z:0} D:0.5} //I believe D should equal -0.5?
Distance from point 1 to plane:
1.0
It seems that your Plane is implemented so that D is not the projection of one of your points onto the plane normal, but rather the negative of this. You can think of this as projecting a vector from the plane to the origin onto the normal.
In any case, I believe that changing
float value = dot - plane.D;
to
float value = dot + plane.D;
should fix things. HTH.
Ok, I'm not totally sure I understand the math here, but I suspect (based on formulas from http://mathworld.wolfram.com/Point-PlaneDistance.html among others) that
float value = dot - plane.D;
should actually be
float value = dot / plane.D;
EDIT: Ok, as mentioned in comments below, this didn't work. My best suggestion then is to go look at the link or google "distance between a point and a plane" and try implementing the formula a different way.

Categories

Resources