## Pages

### [Geometry] Simple code for geometry

You guys can use all this codes in this blog. If you have questions, just comment or send emails to nnbminh@gmail.com (I suggest sending emails).I will help you with my best.

Coding - somehow - makes things simple. I'm doing a project which is the same with Paint in Windows OS. The first step, I must realize the type of geometry. This codes below will do that:
I code in C++

// author: BinhMinh

#include <iostream>
#include <math.h>
#define e 1e-6
using namespace std;

struct Point{
double x;
double y;
};

struct Vector{
double x;
double y;
};

struct Line{
double a;
double b;
double c;
};

double abs(double x){
if(x<0) return -x;
return x;
}

//Area of triangle ABC, Newton
double Area(Point A, Point B, Point C){
double t1 = A.x*B.y + B.x*C.y + C.x*A.y;
double t2 = A.y*B.x + B.y*C.x + C.y*A.x;
if(t1>t2) return (t1-t2)/2.0;
return (t2-t1)/2.0;
}

//Distance between 2 Points
double Dist(Point A, Point B){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

//Distance between Point X and line d(AB)
double DistPointLine(Point C, Point A, Point B){
double s = Area(C, A, B);
double a = Dist(A, B);
return 2.0*s/a;
}

//Make Vector AB
void MakeVector(Point A, Point B, Vector &AB){
AB.x = B.x-A.x;
AB.y = B.y - A.y;
}

//dot vector a and vector b
double Dot(Vector a, Vector b){
return a.x*b.x + a.y*b.y;
}

//dot vector AB and vector CD
double Dot(Point A, Point B, Point C, Point D){
Vector AB, CD;
MakeVector(A, B, AB);
MakeVector(C, D, CD);
return Dot(AB, CD);
}

//Minimum distance between Point C and segment AB
double DistPointSegment(Point C, Point A, Point B){
double m;
if(Dot(A,B,A,C)<=0 || Dot(B,C,B,A)<=0){
return min(Dist(A,C), Dist(B,C));
}
else return DistPointLine(C, A, B);
}

//Area of Polygon, N>=3
double AreaOfPolygon(int N, Point P[]){
double s = 0.0;
for(int i = 2; i<N ; i++) s+=Area(P, P[i-1], P[i]);
return s;
}

//Make Line a*x+b*y=c
void MakeLine(Point A, Point B, Line &d){
d.a = B.y - A.y;
d.b = A.x - B.x;
d.c = A.x*d.a + A.y*d.b;
}

//doubleersection of 2 lines
int Intersection2Line(Line d1, Line d2, Point &P){
double delta = d1.a*d2.b - d2.a*d1.b;
if(abs(delta)<e) return 0; // No doubleersection
P.x = (d1.c*d2.b - d2.c*d1.b)/delta;
P.y = (d1.a*d2.c - d2.a*d1.c)/delta;
return 1;
}

//doubleersection of 2 Segments AB and CD
//AB = A + (B-A)t = A + ut
//CD = C + (D-C)t' = C + vt'
//t = (C-A).vT / u.vT
int Intersection2Segment(Point A, Point B, Point C, Point D, Point &P){
Vector u, v, vT, AC;
MakeVector(A, B, u);
MakeVector(C, D, v);
MakeVector(A, C, AC);
vT.x = -v.y;
vT.y = v.x;
if (abs(Dot(u, vT))<e) return 0;
double t = Dot(AC, vT) / Dot(u, vT);
if (t<0 || t>1) return 0;
P.x = A.x + u.x*t;
P.y = A.y + u.y*t;
}

//Circle from 3 Points
void Bisector(Point A, Point B, Line &d){
double x, y;
x = (A.x + B.x)/2.0;
y = (A.y + B.y)/2.0;
d.a = B.x - A.x;
d.b = B.y - A.y;
d.c = d.a*x + d.b*y;
}
int Circle(Point A, Point B, Point C, Point &I, double R){
Line d1, d2;
Bisector(A, B, d1);
Bisector(A, C, d2);
if( Intersection2Line(d1, d2, I) == 0 ) return 0;
R = Dist(I, A);
return 1;
}

#### 2 nhận xét:

1. Nặc danh23:21

i'm doing an assignment which is abt geometry like your code, but in java. Can you post a tutorial to this one (in java) ?
Thanks so much

2. well, I think it's the same in java. Use class and method instead of function :)
Try to code and contact me if u have any trouble
Good luck !