Skip to content

Commit ef80eea

Browse files
committed
add geom/line.cpp
1 parent f58005f commit ef80eea

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ AtCoder解説放送ライブラリ集
4646
|:--|:--|:--|
4747
|基本|[geom.cpp](geom.cpp)|幾何のベース+目次|
4848
|Vector|[geom/vector.cpp](geom/vector.cpp)|ベクトル(点を扱う際にも使う)|
49+
|Line|[geom/line.cpp](geom/line.cpp)|直線・線分|
4950
|Circle|[geom/circle.cpp](geom/circle.cpp)||

geom.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ const double eps = 1e-9;
33
bool equal(double a, double b) { return abs(a-b) < eps;}
44

55
[geom/vector]
6+
[geom/line]
67
[geom/circle]

geom/line.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Line {
2+
V s, t;
3+
Line(V s=V(0,0), V t=V(0,0)):s(s),t(t){}
4+
V dir() const { return t-s;}
5+
V normalize() const { return dir().normalize();}
6+
double norm() const { return dir().norm();}
7+
/* +1: s-t,s-p : ccw
8+
* -1: s-t,s-p : cw
9+
* +2: t-s-p
10+
* -2: s-t-p
11+
* 0: s-p-t */
12+
int ccw(const V& p) const {
13+
if (dir().cross(p-s) > eps) return +1;
14+
if (dir().cross(p-s) < -eps) return -1;
15+
if (dir().dot(p-s) < -eps) return +2;
16+
if (dir().norm()+eps < (p-s).norm()) return -2;
17+
return 0;
18+
}
19+
bool touch(const Line& l) const {
20+
int a = ccw(l.s)*ccw(l.t), b = l.ccw(s)*l.ccw(t);
21+
return !a || !b || (a == -1 && b == -1);
22+
}
23+
};

geom/vector.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct V {
1515
double cross(const V& v) const { return x*v.y - v.x*y;}
1616
double norm2() const { return x*x + y*y;}
1717
double norm() const { return sqrt(norm2());}
18+
V normalize() const { return *this/norm();}
1819
V rotate90() const { return V(y, -x);}
1920
int ort() const { // orthant
2021
if (abs(x) < eps && abs(y) < eps) return 0;

0 commit comments

Comments
 (0)