1

I need to resolve this by using the least amount of calculation possible using C#.

I have the points (x0, y0); (y1, y1); (x2, y2):   (x1, y1); (x2, y2) define a line, but is an segment "S".   and (x0, y0) is an isolated point, and the distance "d" shorter segment is a segment perpendicular, which has a distance "d".

I have calculated "d" using this formula http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html and also calculated the value of "r" using "Distance between two points".

    public static Double GetDistance(Point sourcePoint, Point destinyPoint)
    {
        Double horizontalDistance = (destinyPoint.X - sourcePoint.X);
        Double verticalDistance = (destinyPoint.Y - sourcePoint.Y);
        Double distance = System.Math.Sqrt((horizontalDistance * horizontalDistance) + (verticalDistance * verticalDistance));
        return distance;
    }

Actually I need to find the coordinates of the red dot.

enter image description here

Cheva
  • 331
  • 5
  • 12
  • Use dot product between the `x1,y1->x2,y2` and `x1,y1->x0,y0` vectors to get angle at `redDot`,`1`,`0` (let's call it `angle1`, lower left dot). Angle between `0`,`redDot`,`1` is 90 (red dot), therefore angle of `1`,`0`,`redDot` (right point) is `90-angle1`. You have length of `d`, now you have angle too, so you have a vector of known length and position, so `redDot` coordinates can be found using [this answer](http://stackoverflow.com/questions/1638437/given-an-angle-and-length-how-do-i-calculate-the-coordinates). Where `0` is `(x0, y0)`, `1` is `(x1, y1)`, etc.. – Quantic Apr 07 '16 at 18:46
  • Can you post some pseudocode? to understand a little more. – Cheva Apr 07 '16 at 19:06
  • Eh.. my math is super rusty, which has got me feeling down. But here's a super crappy picture to look at: [picture](http://i.imgur.com/3id7r6T.png). Note you'll have to shift the `C` vector from the origin to (x0, y0), then find its endpoint (or do vice-versa, doesn't matter). Also I got the polar coordinates backwards, it goes (length, angle), and I put (angle, length). – Quantic Apr 07 '16 at 19:23
  • If you can use vectors (or implement your own Vector), you can solve it easily with [orthogonal projection](https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line) – libertylocked Apr 07 '16 at 19:56

1 Answers1

2

First you can find s easily with

double s = Math.Sqrt(r*r - d*d);

Then find the distance between point1 and point2

double distance = GetDistance(point1, point2);

Now you can solve for the position of the red dot.

double redX = (x2-x1) / distance * s;
double redY = (y2-y1) / distance * s;

Easy method

If you can use vectors (like Vector2 in System.Numerics.Vectors), it will be a very simple orthogonal projection problem.

Vector2 p1 = new Vector2(point1.X, point1.Y);
Vector2 p2 = new Vector2(point2.X, point2.Y);
Vector2 p0 = new Vector2(point0.X, point0.Y);

Vector2 s = p2 - p1;
Vector2 v = p0 - p1;

double len = Vector2.Dot(v, s) / Vector2.Dot(s, s);
Vector2 projection = len * s;

// Finally get the position of your red dot
Point pointRedDot = point1 + new Point(projection.X, projection.Y);

Console.WriteLine(pointRedDot);
libertylocked
  • 892
  • 5
  • 15