Skip to content
Snippets Groups Projects
Commit c086fbb7 authored by Yuriy Maksymets's avatar Yuriy Maksymets
Browse files

Refactored geometry functions

parent 7b7e0442
No related branches found
No related tags found
2 merge requests!57Erasure intervals,!48Intuitive erasing
function distance([x1, y1], [x2, y2]) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
}
function sqr(x) {
return x ** 2
}
function dist2(v, w) {
return sqr(v[0] - w[0]) + sqr(v[1] - w[1])
function hypotenuseSquared(a, b) {
return sqr(a) + sqr(b)
}
function distanceSquared([x0, y0], [x1, y1]) {
return hypotenuseSquared(x0 - x1, y0 - y1)
}
// p - point
// v - start point of segment
// w - end point of segment
function distToSegmentSquared(p, v, w) {
var l2 = dist2(v, w)
if (l2 === 0) return dist2(p, v)
var t = ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l2
function distance(point0, point1) {
return Math.sqrt(distanceSquared(point0, point1))
}
function distToSegmentSquared(point, lineStart, lineEnd) {
var l2 = distanceSquared(lineStart, lineEnd)
if (l2 === 0) return distanceSquared(point, lineStart)
var t =
((point[0] - lineStart[0]) * (lineEnd[0] - lineStart[0]) +
(point[1] - lineStart[1]) * (lineEnd[1] - lineStart[1])) /
l2
t = Math.max(0, Math.min(1, t))
return dist2(p, [v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])])
return distanceSquared(point, [
lineStart[0] + t * (lineEnd[0] - lineStart[0]),
lineStart[1] + t * (lineEnd[1] - lineStart[1]),
])
}
// p - point
// v - start point of segment
// w - end point of segment
function distToSegment(p, v, w) {
return Math.sqrt(distToSegmentSquared(p, v, w))
function distToSegment(point, lineStart, lineEnd) {
return Math.sqrt(distToSegmentSquared(point, lineStart, lineEnd))
}
function distanceToLine(lineStart, lineEnd, point) {
......@@ -38,12 +42,12 @@ function shouldAddErasureInterval(
erasureCenter,
erasureRadius,
) {
if (!nextPoint) return false
if (!(point && nextPoint && erasureCenter)) return false
return distanceToLine(point, nextPoint, erasureCenter) <= erasureRadius
}
function interpolate([x1, y1], [x2, y2], t) {
return [x1 + (x2 - x1) * t, y1 + (y2 - y1) * t]
function interpolate([x0, y0], [x1, y1], t) {
return [x0 + (x1 - x0) * t, y0 + (y1 - y0) * t]
}
function project([x1, y1], [x2, y2], [x3, y3]) {
......@@ -54,12 +58,8 @@ function project([x1, y1], [x2, y2], [x3, y3]) {
return (x31 * x21 + y31 * y21) / (x21 * x21 + y21 * y21)
}
function cap0(x) {
return Math.max(0, cap1(x))
}
function cap1(x) {
return Math.min(1, x)
function cap01(x) {
return Math.max(0, Math.min(1, x))
}
function erasureInterval(lineStart, lineEnd, erasureCenter, erasureRadius) {
......@@ -75,14 +75,14 @@ function erasureInterval(lineStart, lineEnd, erasureCenter, erasureRadius) {
project(lineStart, lineEnd, erasureCenter),
)
const touchFromStartDist = distance(lineStart, projectionPoint)
const halfLength = Math.sqrt(erasureRadius ** 2 - distToLine ** 2)
const halfLength = Math.sqrt(sqr(erasureRadius) - sqr(distToLine))
const touchBeginFromStarDist = touchFromStartDist - halfLength
const touchEndFromStarDist = touchFromStartDist + halfLength
return [
cap0(touchBeginFromStarDist / lineLength),
cap1(touchEndFromStarDist / lineLength),
cap01(touchBeginFromStarDist / lineLength),
cap01(touchEndFromStarDist / lineLength),
]
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment