From 1d2d2541dd913892a983da13f84c33f1a5d43ff6 Mon Sep 17 00:00:00 2001 From: Paul Jungeblut Date: Tue, 11 Nov 2014 15:25:06 +0100 Subject: geometrische Formeln --- geometry/formulars.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ geometry/geometry.tex | 7 ++++-- 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 geometry/formulars.cpp (limited to 'geometry') diff --git a/geometry/formulars.cpp b/geometry/formulars.cpp new file mode 100644 index 0000000..6984be9 --- /dev/null +++ b/geometry/formulars.cpp @@ -0,0 +1,58 @@ +//komplexe Zahlen als Darstellung fuer Punkte +typedef pt complex; +//Winkel zwischen Punkt und x-Achse in [0, 2 * PI), Winkel zwischen a und b +double angle = arg (a), angle_a_b = arg (a - b); +//Punkt rotiert um Winkel theta +pt a_rotated = a * exp (pt (0, theta)); +//Mittelpunkt des Dreiecks abc +pt centroid = (a + b + c) / 3; +//Skalarprodukt +double dot(pt a, pt b) { + return real(conj(a) * b); +} +//Kreuzprodukt, 0, falls kollinear +double cross(pt a, pt b) { + return imag(conj(a) * b); +} +//wenn Eckpunkte bekannt +double areaOfTriangle(pt a, pt b, pt c) { + return abs(cross(b - a, c - a)) / 2.0; +} +//wenn Seitenlaengen bekannt +double areaOfTriangle(double a, double b, double c) { + double s = (a + b + c) / 2; + return sqrt(s * (s-a) * (s-b) * (s-c)); +} +// Sind die Dreiecke a1, b1, c1, and a2, b2, c2 aehnlich? +// Erste Zeile testet Aehnlichkeit mit gleicher Orientierung, +// zweite Zeile testst Aehnlichkeit mit unterschiedlicher Orientierung +bool similar (pt a1, pt b1, pt c1, pt a2, pt b2, pt c2) { + return ( + (b2 - a2) * (c1 - a1) == (b1 - a1) * (c2 - a2) || + (b2 - a2) * (conj (c1) - conj (a1)) == (conj (b1) - conj (a1)) * (c2 - a2) + ); +} +//Linksknick von a->b nach a->c +double ccw(pt a, pt b, pt c) { + return cross(b - a, c - a); //<0 => falls Rechtsknick, 0 => kollinear, >0 => Linksknick +} +//Streckenschnitt, Strecken a-b und c-d +bool lineSegmentIntersection(pt a, pt b, pt c, pt d) { + if (ccw(a, b, c) == 0 && ccw(a, b, d) == 0) { //kollinear + double dist = abs(a - b); + return (abs(a - c) <= dist && abs(b - c) <= dist) || (abs(a - d) <= dist && abs(b - d) <= dist); + } + return ccw(a, b, c) * ccw(a, b, d) <= 0 && ccw(c, d, a) * ccw(c, d, b) <= 0; +} +//Entfernung von p zu a-b +double distToLine(pt a, pt b, pt p) { + return abs(cross(p - a, b - a)) / abs(b - a); +} +//liegt p auf a-b +bool pointOnLine(pt a, pt b, pt p) { + return abs(distToLine(a, b, p)) < EPSILON; +} +//testet, ob d in der gleichen Ebene liegt wie a, b, und c +bool isCoplanar(pt a, pt b, pt c, pt d) { + return (b - a) * (c - a) * (d - a) == 0; +} \ No newline at end of file diff --git a/geometry/geometry.tex b/geometry/geometry.tex index 7a72526..6192c6e 100644 --- a/geometry/geometry.tex +++ b/geometry/geometry.tex @@ -1,4 +1,7 @@ -\section{Geometry} +\section{Geometrie} \subsection{Closest Pair} -\lstinputlisting{geometry/closestPair.cpp} \ No newline at end of file +\lstinputlisting{geometry/closestPair.cpp} + +\subsection{Formeln - \lstinline{std::complex}} +\lstinputlisting{geometry/formulars.cpp} \ No newline at end of file -- cgit v1.2.3