guest@cp-base:~/home/geometry$
templates/basic-geometry-checks.cpp
compilable
$cat templates/basic-geometry-checks

Basic Geometry Checks

Triangle validity, Euclidean distance, and collinearity check using cross product.

[Geometry]|
Jul 9, 2026
|explanatory_notes.md|
#geometry#triangle#distance#collinear#cross-product
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

bool isTriangle(ll a, ll b, ll c) {
    if (a <= 0 || b <= 0 || c <= 0) return false;
    return a + b > c && a + c > b && b + c > a;
}

double dist(double x1, double y1, double x2, double y2) {
    double dx = x1 - x2, dy = y1 - y2;
    return sqrt(dx * dx + dy * dy);
}

bool isCollinear(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) {
    return (y2 - y1) * (x3 - x1) == (y3 - y1) * (x2 - x1);
}
17 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

Basic Geometry Checks

Functions

  • isTriangle(a, b, c) — checks if three side lengths form a valid triangle (all positive, triangle inequality)

  • dist(x1, y1, x2, y2) — Euclidean distance: d=(x1x2)2+(y1y2)2d = \sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}

  • isCollinear(x1, y1, x2, y2, x3, y3) — checks if three points are collinear using cross product (integer-exact, no floating-point):
  • (y2y1)(x3x1)=(y3y1)(x2x1)(y_2 - y_1)(x_3 - x_1) = (y_3 - y_1)(x_2 - x_1)

    Notes

  • isCollinear avoids slope computation, handles vertical lines correctly

  • Intermediate products can reach 1018\sim 10^{18} with coordinates up to 10910^9 — fits in ll. Use __int128 for larger coords

  • For more complex geometry, see 2D Geometry and Convex Hull templates
  • Complexity

  • All functions — O(1)O(1)