summaryrefslogtreecommitdiffstats
path: root/geometry.h
blob: a272b2c63259e835bcf2425988a863634ac7b1b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef GEOMETRY_H_
#define GEOMETRY_H_


#include <glib.h>

#define EDGE_NONE        0
#define EDGE_LEFT   (1<<0)
#define EDGE_RIGHT  (1<<1)
#define EDGE_TOP    (1<<2)
#define EDGE_BOTTOM (1<<3)
#define EDGE_ALL    (EDGE_LEFT|EDGE_RIGHT|EDGE_TOP|EDGE_BOTTOM)

#define INTERSECTION_ERROR           -1
#define INTERSECTION_NONE             0
#define INTERSECTION_IDENTICAL        1
#define INTERSECTION_LINE             2
#define INTERSECTION_LINE_LINE        2
#define INTERSECTION_LINE_SEGMENT     3
#define INTERSECTION_SEGMENT_LINE     6
#define INTERSECTION_SEGMENT_SEGMENT  7


typedef struct _VERTEX {
  double x, y;
} VERTEX;

typedef struct _RECTANGLE {
  double x, y, width, height;
} RECTANGLE;

typedef struct _LINE {
  VERTEX v1, v2;
} LINE;

typedef struct _VERTEX_LIST {
  unsigned int nVertices;
  VERTEX *vertices;
} VERTEX_LIST, POLYGON;


void addVertex(VERTEX_LIST *list, const VERTEX *v);
void insertVertex(VERTEX_LIST *list, const VERTEX *v, unsigned int n);
void deleteVertex(VERTEX_LIST *list, unsigned int n);

double vertexDistanceSquare(const VERTEX *v1, const VERTEX *v2);
double vertexDistance(const VERTEX *v1, const VERTEX *v2);

int vertexOnLine(const VERTEX *v, const LINE *l);
int vertexInRect(const VERTEX *v, const RECTANGLE *rect);
gboolean vertexInPolygon(const VERTEX *v, const POLYGON *p);

double polygonPerimeter(const POLYGON *p);
double polygonArea(const POLYGON *p);

int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v);
int lineRectIntersection(const LINE *l, const RECTANGLE *rect, int edge, VERTEX *v);
int lineRectIntersections(const LINE *line, const RECTANGLE *rect, int edge, VERTEX *v1, VERTEX *v2);
gboolean linePolygonIntersection(const LINE *l, const POLYGON *p);
void simplifyPolygon(const POLYGON *in, const RECTANGLE *rect, POLYGON *out);

#endif /*GEOMETRY_H_*/