Skip to content

Instantly share code, notes, and snippets.

@Frityet
Last active November 30, 2021 20:20
Show Gist options
  • Save Frityet/05cad6d91f4fe0ef7294b4019ac20c6e to your computer and use it in GitHub Desktop.
Save Frityet/05cad6d91f4fe0ef7294b4019ac20c6e to your computer and use it in GitHub Desktop.
Checks if a triangle is equilateral
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
typedef struct {
uint16_t x, y;
} vector2_t;
#define POINT(_x, _y) (vector2_t) { .x = (_x), .y = (_y) }
static inline double distance_between_points(vector2_t point1, vector2_t point2)
{
/*
* Distance formula:
*
* /-------------------------
* / (x2 - x1)^2 + (y2 - y1)^2
* \/
*
*/
return ceil(sqrt(pow(point2.x - point1.x, 2) + pow(point2.y - point1.y, 2)));
}
bool is_equilateral_points(vector2_t point1, vector2_t point2, vector2_t point3)
{
double side1 = distance_between_points(point1, point2),
side2 = distance_between_points(point2, point3),
side3 = distance_between_points(point1, point3);
return (side1 == side2) && (side2 == side3);
}
inline static char *bstr(bool b)
{
return b ? "true" : "false";
}
int main()
{
printf("%s\n", bstr(is_equilateral_points(POINT(2, 1), POINT(5, 5), POINT(7, 1))));
printf("%s\n", bstr(is_equilateral_points(POINT(0, 2), POINT(1, 3), POINT(2, 0))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment