Skip to content

Commit ba01116

Browse files
authored
Playground styles (#3)
* Playground styles * Readme
1 parent 5aa6ac2 commit ba01116

File tree

6 files changed

+77
-26
lines changed

6 files changed

+77
-26
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# `segment-intersection`
22

3+
Fast segment 2D intersection, that includes edge cases, such as segments touching points, or one segment being on top of the other.
4+
35
## API
46

57
```ts
@@ -10,6 +12,19 @@ console.log(isect); // 0, 1 or 2
1012
console.log(intersection); // [[x, y], [x, y]]
1113
```
1214

15+
If `isect === 1`, your intersection point is `intersection[0]`.
16+
17+
Also, you can write into an existing array:
18+
19+
```ts
20+
import { findIntersection } from 'segment-intersection';
21+
const intersection = [
22+
[0, 0],
23+
[0, 0],
24+
];
25+
const isect = findIntersection(x0, y0, x1, y1, x2, y2, x3, y3, intersection);
26+
```
27+
1328
### Benchmark
1429

1530
```

playground/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
</head>
88
<body>
99
<script type="module" src="./main.ts"></script>
10+
<iframe
11+
id="fork-button"
12+
src="https://ghbtns.com/github-btn.html?user=w8r&repo=2d-segment-intersection&type=star&count=true"
13+
frameborder="0"
14+
scrolling="0"
15+
width="150"
16+
height="20"
17+
title="GitHub"
18+
></iframe>
19+
<div id="output"></div>
1020
</body>
1121
</html>

playground/main.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ const height = window.innerHeight;
2121
const resolution = 20;
2222
const r = 15;
2323

24-
const points = new Array(4).fill(0).map((_, i) => {
25-
return {
26-
x: round(Math.random() * (width - r * 2), resolution),
27-
y: round(Math.random() * (height - r * 2), resolution),
28-
index: i,
29-
};
30-
});
24+
const points = [
25+
{ x: 280, y: 260, index: 0 },
26+
{ x: 580, y: 120, index: 1 },
27+
{ x: 380, y: 130, index: 2 },
28+
{ x: 580, y: 240, index: 3 },
29+
];
3130

3231
const drag = d3Drag<SVGCircleElement, Point>().on('drag', dragged);
3332
const svg = d3Select('body')
@@ -113,6 +112,14 @@ function dragged(
113112

114113
d3Select(this).attr('cx', d.x).attr('cy', d.y);
115114

115+
highlightIntersections();
116+
}
117+
118+
function round(p: number, n: number) {
119+
return p % n < n / 2 ? p - (p % n) : p + n - (p % n);
120+
}
121+
122+
function highlightIntersections() {
116123
const isect = findIntersection(
117124
points[0].x,
118125
points[0].y,
@@ -134,13 +141,23 @@ function dragged(
134141
intersectionPoints.classed('hidden', true);
135142
} else if (isect === 1) {
136143
// 1 intersection point
144+
intersectionPoints.classed('hidden', true);
137145
intersectionPoints.filter((_, i) => i === 0).classed('hidden', false);
138146
} else if (isect === 2) {
139147
// 2 intersection points
140148
intersectionPoints.classed('hidden', false);
141149
}
150+
document.getElementById('output')!.innerHTML =
151+
isect === 0
152+
? ''
153+
: isect === 1
154+
? `${formatPoint(intersection[0])}`
155+
: `${formatPoint(intersection[0])},\n${formatPoint(intersection[1])}`;
156+
console.log('Intersection: ', isect);
142157
}
143158

144-
function round(p: number, n: number) {
145-
return p % n < n / 2 ? p - (p % n) : p + n - (p % n);
159+
function formatPoint([x, y]: [number, number]) {
160+
return `${x.toFixed(2)}, ${y.toFixed(2)}`;
146161
}
162+
163+
highlightIntersections();

playground/style.css

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
body {
22
margin: 0;
33
padding: 0;
4-
background: #eee;
4+
background: rgb(242, 242, 242);
55
width: 100vw;
66
height: 100vh;
77
display: flex;
@@ -12,17 +12,17 @@ body {
1212

1313
svg {
1414
box-sizing: border-box;
15-
border: 1px solid rgb(212, 212, 212);
1615
}
1716

1817
circle {
1918
stroke: rgb(95, 176, 228);
2019
stroke-width: 2;
2120
fill: rgba(205, 246, 255);
21+
cursor: pointer;
2222
}
2323

2424
line {
25-
stroke: rgb(212, 212, 212);
25+
stroke: #ddd;
2626
stroke-width: 1px;
2727
}
2828

@@ -35,6 +35,24 @@ line {
3535
display: none;
3636
}
3737

38+
#fork-button {
39+
position: absolute;
40+
top: 20px;
41+
left: 20px;
42+
cursor: pointer;
43+
}
44+
3845
.intersection {
3946
fill: rgba(205, 46, 55, 0.85);
4047
}
48+
49+
#output {
50+
position: absolute;
51+
top: 60px;
52+
left: 20px;
53+
font-size: 14px;
54+
background-color: #dedede;
55+
padding: 10px;
56+
width: 120px;
57+
font-family: Georgia, 'Times New Roman', Times, serif;
58+
}

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function findIntersection(
1616
p1y: number,
1717
d1x: number,
1818
d1y: number,
19+
out = result,
1920
sqrEpsilon = 1e-6
2021
): number {
2122
// segments P0 + s * D0 for s in [0, 1], P1 + t * D1 for t in [0,1]
@@ -47,7 +48,7 @@ export function findIntersection(
4748
// intersection of lines is not a point on segment P1 + t * D1
4849
if (t < 0 || t > 1) return 0;
4950

50-
const res0 = result[0];
51+
const res0 = out[0];
5152
// intersection of lines is a point on each segment
5253
if (s === 0 || s === 1) {
5354
// on an endpoint of line segment a
@@ -114,12 +115,12 @@ export function findIntersection(
114115
}
115116

116117
if (imax > 0) {
117-
const res0 = result[0];
118+
const res0 = out[0];
118119
res0[0] = p0x + w0 * vax;
119120
res0[1] = p0y + w0 * vay;
120121

121122
if (imax === 2) {
122-
const res1 = result[1];
123+
const res1 = out[1];
123124
res1[0] = p0x + w1 * vax;
124125
res1[1] = p0y + w1 * vay;
125126
}

tests/unit.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,7 @@ describe('segment intersection', () => {
136136
).toBe(2);
137137
// fails due to floating point error
138138
expect(
139-
findIntersection(
140-
0,
141-
0,
142-
1e20,
143-
1e-11,
144-
1,
145-
0,
146-
100000000000000020000,
147-
1e-12,
148-
1e-20
149-
)
139+
findIntersection(0, 0, 1e20, 1e-11, 1, 0, 100000000000000020000, 1e-12)
150140
).toBe(2);
151141
});
152142
});

0 commit comments

Comments
 (0)