summaryrefslogtreecommitdiffstats
path: root/Line.cpp
blob: aa7bcad574f675c84d38fba6d62045b8cb70dd57 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Line.h"
#include <math.h>


bool Line::contains(const Vertex &v) const {
  if(fabsf(v1.getX() - v2.getX()) < 1E-6 && fabsf(v1.getY() - v2.getY()) < 1E-6) {
    if(fabsf(v1.getX() - v.getX()) < 1E-6 && fabsf(v1.getY() - v.getY()) < 1E-6)
      return true;
    else
      return false;
  }
  
  if(fabsf(v1.getX() - v2.getX()) < 1E-6) {
    if(fabsf(v1.getX() - v.getX()) >= 1E-6)
      return false;
    else if(v.getY() - fminf(v1.getY(), v2.getY()) > -1E-6 && v.getY() - fmaxf(v1.getY(), v2.getY()) < 1E-6)
      return true;
    else
      return false;
  }
  
  if(fabsf(v1.getY() - v2.getY()) < 1E-6) {
    if(fabsf(v1.getY() - v.getY()) >= 1E-6)
      return false;
    else if(v.getX() - fminf(v1.getX(), v2.getX()) > -1E-6 && v.getX() - fmaxf(v1.getX(), v2.getX()) < 1E-6)
      return true;
    else
      return false;
  }
  if(fabsf((v.getX()-v1.getX())/(v2.getX()-v1.getX()) - (v.getY()-v1.getY())/(v2.getY()-v1.getY())) < 1E-6)
    return true;
  else
    return false;
}

int Line::intersects(const Line &l, Vertex *v) const {
  float xa1 = v1.getX(), ya1 = v1.getY();
  float xa2 = v2.getX(), ya2 = v2.getY();
  float xb1 = l.v1.getX(), yb1 = l.v1.getY();
  float xb2 = l.v2.getX(), yb2 = l.v2.getY();
  float temp;
  bool switched = false;
  Vertex v2;
  
  
  if(!v) v = &v2;
  
  if(fabsf(xa1 - xa2) < 1E-6 && fabsf(ya1 - ya2) < 1E-6) return INTERSECTION_ERROR;
  if(fabsf(xb1 - xb2) < 1E-6 && fabsf(yb1 - yb2) < 1E-6) return INTERSECTION_ERROR;
  
  if(fabsf(xa1 - xa2) < 1E-6 || fabsf(xb1 - xb2) < 1E-6) {
    temp = xa1; xa1 = ya1; ya1 = temp;
    temp = xa2; xa2 = ya2; ya2 = temp;
    temp = xb1; xb1 = yb1; yb1 = temp;
    temp = xb2; xb2 = yb2; yb2 = temp;
    
    switched = true;
  }
  
  if(fabsf(xa1 - xa2) < 1E-6 && fabsf(xb1 - xb2) < 1E-6)
    return (fabsf(xa1 - xb1) < 1E-6) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE;
  
  if(fabsf(xa1 - xa2) < 1E-6) {
    v->setX(xa1);
    v->setY(yb1);
  }
  else if(fabsf(xb1 - xb2) < 1E-6) {
    v->setX(xb1);
    v->setY(ya1);
  }
  else {
    float ma = (ya2-ya1)/(xa2-xa1);
    float mb = (yb2-yb1)/(xb2-xb1);
    float ba = ya1 - ma*xa1;
    float bb = yb1 - mb*xb1;
    
    if(fabsf(ma - mb) < 1E-6) return (fabsf(ba - bb) < 1E-6) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE;
    
    v->setX((bb-ba)/(ma-mb));
    v->setY(ma*v->getX() + ba);
  }
  
  if(switched) {
    temp = v->getX(); v->setX(v->getY()); v->setY(temp);
    
    //switch back everything for segment tests
    temp = xa1; xa1 = ya1; ya1 = temp;
    temp = xa2; xa2 = ya2; ya2 = temp;
    temp = xb1; xb1 = yb1; yb1 = temp;
    temp = xb2; xb2 = yb2; yb2 = temp;
  }
  
  if(v->getX() < fminf(xa1,xa2) || v->getX() > fmaxf(xa1, xa2) || v->getY() < fminf(ya1,ya2) || v->getY() > fmaxf(ya1, ya2)) {
    if(v->getX() < fminf(xb1,xb2) || v->getX() > fmaxf(xb1, xb2) || v->getY() < fminf(yb1,yb2) || v->getY() > fmaxf(yb1, yb2))
      return INTERSECTION_LINE_LINE;
    else
      return INTERSECTION_LINE_SEGMENT;
  }
  else if(v->getX() < fminf(xb1,xb2) || v->getX() > fmaxf(xb1, xb2) || v->getY() < fminf(yb1,yb2) || v->getY() > fmaxf(yb1, yb2))
    return INTERSECTION_SEGMENT_LINE;
  else
    return INTERSECTION_SEGMENT_SEGMENT;
}