How to find coordinates of a 2d equilateral triangle in C?

14,076

Solution 1

After reading the posts (specially vkit's) I produced this simple piece of code which will do the trick for one direction (remember that there are two points). The modification for the other case shold be trivial.

#include<stdio.h>
#include<math.h>

typedef struct{
  double x;
  double y;
} Point;

Point vertex(Point p1, Point p2){
  double s60 = sin(60 * M_PI / 180.0);    
  double c60 = cos(60 * M_PI / 180.0);

  Point v = {
    c60 * (p1.x - p2.x) - s60 * (p1.y - p2.y) + p2.x,
    s60 * (p1.x - p2.x) + c60 * (p1.y - p2.y) + p2.y
  };

  return v;
}

Solution 2

You could rotate the second point 60° around first to find the location of the third point.

Something like this:

//find offset from point 1 to 2
dX = x2 - x1;
dY = y2 - y1;
//rotate and add to point 1 to find point 3
x3 = (cos(60°) * dX - sin(60°) * dY) + x1;
y3 = (sin(60°) * dX + cos(60°) * dY) + y1;

Solution 3

Let's call your two points A and B. Bisect AB, call this point C. Find the slope of AB (YA-YB / XA-XB), call it m. Find the perpendicular to that (-1/m) and call it m2. Then compute a segment CD whose length is sin(60) * length(AB), at the slope m2 (there will be two such points, one to each side of AB). ABD is then your equilateral triangle.

That, obviously, is a "constructive" method. You should also be able to do it by solving a set of linear equations. I haven't tried to figure out the right system of equations for this case, but this approach tends to be somewhat more stable numerically, and has fewer special cases (e.g., with the constructive version, a slope of 0 has to be dealt with specially).

Share:
14,076

Related videos on Youtube

Horatiu Jeflea
Author by

Horatiu Jeflea

Backend: Python - Django Java - Spring, Hibernate Node - Express, Typescript Frontend: React Redux Cloud: AWS Azure Heroku Other: Serverless Big Data NLU/Chatbot Startups Articles: https://medium.com/@horatiujeflea Public projects: https://github.com/horatiujeflea CV: LinkedIn Mail: [email protected]

Updated on April 22, 2022

Comments

  • Horatiu Jeflea
    Horatiu Jeflea about 2 years

    I have the coordinates (x,y) of 2 points. I want to build the third point so that these 3 points make an equilateral triangle.

    How can I calculate the third point?

    Thank you

    • James McNellis
      James McNellis about 14 years
      FYI: There are two such points.
    • MSN
      MSN about 14 years
      If the two points have the same x coordinate, there is no "upper point".
    • BlueRaja - Danny Pflughoeft
      BlueRaja - Danny Pflughoeft about 14 years
      First one to do this without trig functions gets my upvote :)
  • Escualo
    Escualo about 14 years
    Isn't it [cos(60) sin(60); -sin(60) cos(60)]? I think your signs are wrong.
  • vkit
    vkit about 14 years
    I think those signs are correct. See also: stackoverflow.com/questions/786472/rotate-a-point-by-an-angl‌​e
  • vkit
    vkit about 14 years
    @arrieta Just tried this out, if you use the signs as you suggest it rotates the point the other way (clockwise/counter-clockwise) so depending on how your coordinate system is set up, one or the other might make more sense.
  • Admin
    Admin about 14 years
    I said the same thing! And I didn't put any explicit code for a reason: graphics API can do rotations (and translations) much better(precision, trigonometric calculations etc etc) than we can ever hope to do ourselves. Since we do not know the API OP is using, all we can do is mention what to do. Oh well...