0

I've gotten pretty far but something just doesn't seem to work.

enter image description here

A = 50.88259382849774,6.003988087177277
B = 50.88269282423443,6.0036662220954895
C = 50.882530369581545,6.003847271203995

The C coordinate is a little off from the 90 degree line (x) and this function I made should position C on the closest way to the x line.

this.snapCoords = function(a, b, c){
    var result = (b.x-a.x)*(c.x-b.x)+(b.y-a.y)*(c.y-b.y);

    var negative = false;
    if(result < 0){
        result = result*-1;
        negative = true;
    }
    result = Math.sqrt(result);
    result = result/2;

    if(negative === false){
        var d = {x: c.x+result, y: c.y-result};
    }
    else{
        var d = {x: c.x-result, y: c.y+result};
    }

    console.log(d); // returns : 50.88246729610898,6.003910344676565
}

It does get the the 90 degree (x) line but not the closest way. Something must still be wrong in my function but I can't figure it out.

EDIT:

So this is my problem

enter image description here

My function puts the third coordinate on C which is 90 degrees but not where it should be (the red spot) it somehow extends to a further point.

Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
  • What exactly is the problem? – Pointy Dec 02 '14 at 14:31
  • 1
    Is your question: why is the "returns : 50.882467..., 6.003910344... different than C=50.882430369..., 6.00384727... above? That could very easily be numerical issues (especially if you're using floats instead of doubles). – TravisJ Dec 02 '14 at 14:39
  • Thats exactly my problem. I editted my question btw. – Sinan Samet Dec 02 '14 at 14:40
  • I don't think it is possible to deduce the location of the red dot only knowing points A, B, and C (labelled on the picture above). If you had the location of that 4th point on that trapezoid then you could. – TravisJ Dec 02 '14 at 14:43
  • You can certainly deduce the line that the red dot lies on, but not the distance it is from B. – TravisJ Dec 02 '14 at 14:44
  • The function works exactly like I want it with shorter coordinates like 0,0 , 4,8 etc but on my real script somehow it doesn't. I know point ABC and when I drop C somewhere near the 90 degree line it is supposed to give the coordinates to the closest spot to the 90 degree line. – Sinan Samet Dec 02 '14 at 14:47
  • It seems to me that it's more likely the numerical problem like you said before. Because I can see that the new coordinates are both 1 number short compared to the old one. – Sinan Samet Dec 02 '14 at 14:50

1 Answers1

1

I think the OP is trying to project the point C onto the line passing thru point B and is perpendicular to line AB. If this is the case, the math for the projection is not correct. You can find the projected point D as

D= C - dot(vec(BC), vec(AB)) * vec(AB)/|vec(AB)|^2

By this calculation, the projected point D will be (50.8825952820492, 6.00363622113846).

The following is a picture for points A, B, C and D :
Pict1

fang
  • 3,473
  • 1
  • 13
  • 19
  • You did understood me correctly but it doesn't work. This is what I get when I put C on the coordinates you gave http://i62.tinypic.com/2zrm4n4.jpg The coordinates are supposed to be very close to 50.882531823134975,6.0038467745462185 (gps google maps coordinates btw) – Sinan Samet Dec 03 '14 at 08:48
  • When I drop the C point on the red spot it should give me the coordinates to the spot thats perpendicular to the line AB like you said. Which would be very close to the red spot but very precisely on the 90 degree angle of AB – Sinan Samet Dec 03 '14 at 08:54
  • Not sure why did you get a different result than mine. I edit my post by adding a picture of all the 3 points A, B and C, and the projected point D. – fang Dec 03 '14 at 18:07
  • Maybe I explained it wrong but point C is supposed to be on the red spot. As you could see in the image it's supposed to be a rectangle but I can't get it right. So what I need to know is the coordinates of the red spot. (Sorry for the late answer I have been trying to figure it out all this time but can't get it right) – Sinan Samet Dec 09 '14 at 15:35
  • Also I don't exactly understand your calculation `D= C - dot(BC, AB) * AB/|AB|^2` Could you perhaps rewrite it in a more simple way? I don't understand what you mean by `dot(BC, AB)` – Sinan Samet Dec 09 '14 at 15:38
  • If you plot the points A, B and C by the coordinates given in your original post, there is no way for point D to be coincident with point C. dot(BC, AB) simply means the inner product of vector(BC) and vector(AB) and |AB| means the magnitude of vector(AB). I edit the formula a bit to reflect this. – fang Dec 09 '14 at 17:45
  • To be honest I still don't understand the calculation I've been trying to fill it in for myselfbut I don't really understand how since I have never worked with the dot product and vectores before. I did look it all up though and I have been experimenting with it. – Sinan Samet Dec 22 '14 at 09:45
  • The "dot(vec(BC), vec(AB)) * vec(AB)/|vec(AB)|^2" portion simply computes the component of vector(BC) in the direction of vec(AB). Hope this helps. – fang Dec 22 '14 at 18:35
  • I actually mean I really can't figure out how to calculate it.. When I try it I get weird answers like 2 which is not even a coordinate. (excuse me for the stupidity) vec(AB) would be sqrt((ax-bx)^2+(ay+by)^2) right? – Sinan Samet Dec 23 '14 at 09:48
  • Let's say I have A=1,5 B=4,8 C=5,3 then D is supposed to be 7,5 but what I get of it is 2,2 with your calculation... – Sinan Samet Dec 23 '14 at 09:59
  • @OP: vec(AB) means the vector defined from point A to point B. So, vec(AB) = B-A. It is not the same as magnitude of vec(AB). – fang Dec 23 '14 at 17:32
  • 1
    @OP: The coordinates of D for your latest example indeed should be (7,5). I calculated with my formula and get (7,5). Here are the result of some intermediate calculation for you to verify your result: dot(vec(BC),vec(AB)) = -12. vec(AB)/|vec(AB)|^2=(1/6, 1/6). So, their product would be (-2, -2) and (5, 3) - (-2, -2) = (7, 5). – fang Dec 23 '14 at 17:36
  • Thank you I figured how to make it work in javascript finally and it all goes well with normal coordinates. But as soon as I put GPS coordinates it just doesn't work it gives back the wrong coordinates somehow. – Sinan Samet Jan 05 '15 at 09:34
  • http://jsfiddle.net/Lmj6vx16/ Here is the function I made and the first one is correct but the second one (the one with real gps coordinates) is not correct somehow. – Sinan Samet Jan 05 '15 at 09:46
  • There was a mistake there this is the better fiddle and it comes close but not on the exact spot http://jsfiddle.net/Lmj6vx16/2/ It's going further than it's supposed to like shown in the last picture of my question. – Sinan Samet Jan 05 '15 at 10:40
  • So the only statement that's not true is "CD is parallel to AB" but only when I use gps coordinates. When I use normal coordinates it works perfectly. – Sinan Samet Jan 05 '15 at 10:49
  • The GPS coordinates and Cartesian coordinates are different. So, yes the same formula that works for Cartesian coordinates will not work for GPS coordinates. If you are dealing with GPS coordinates, it is better to ask a separate question than keep making comments on this question. Furthermore, if the formula I posted works well for you, please give a closure to this question by marking it as "accepted". – fang Jan 05 '15 at 17:52