Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drawing-app
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sweng-group-15
drawing-app
Merge requests
!48
Intuitive erasing
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Intuitive erasing
intuitive-erasing
into
master
Overview
23
Commits
26
Pipelines
12
Changes
1
Merged
Intuitive erasing
Iurii Maksymets
requested to merge
intuitive-erasing
into
master
Oct 31, 2019
Overview
11
Commits
26
Pipelines
12
Changes
1
Closes
!49 (closed)
.
Edited
Nov 1, 2019
by
Nayeem Rahman
0
1
Merge request reports
Viewing commit
043f6d41
Show latest version
1 file
+
19
−
31
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
043f6d41
Refactor distance handling in src/erasure.js
· 043f6d41
Nayeem Rahman
authored
Oct 31, 2019
src/erasure.js
+
19
−
31
View file @ 043f6d41
Edit in single-file editor
Open in Web IDE
Show full file
function
sqr
(
x
)
{
return
x
**
2
function
getDistance
([
x0
,
y0
],
[
x1
,
y1
]
)
{
return
Math
.
hypot
(
x1
-
x0
,
y1
-
y0
)
}
function
hypotenuseSquared
(
a
,
b
)
{
return
sqr
(
a
)
+
sqr
(
b
)
function
clamp
(
x
,
min
,
max
)
{
return
Math
.
max
(
min
,
Math
.
min
(
max
,
x
)
)
}
function
distanceSquared
([
x0
,
y0
],
[
x1
,
y1
])
{
return
hypotenuseSquared
(
x0
-
x1
,
y0
-
y1
)
}
function
distance
(
point0
,
point1
)
{
return
Math
.
sqrt
(
distanceSquared
(
point0
,
point1
))
}
function
cap01
(
x
)
{
return
Math
.
max
(
0
,
Math
.
min
(
1
,
x
))
}
function
distToSegmentSquared
(
lineStart
,
lineEnd
,
point
)
{
const
l2
=
distanceSquared
(
lineStart
,
lineEnd
)
if
(
l2
===
0
)
return
distanceSquared
(
point
,
lineStart
)
function
distToSegment
(
lineStart
,
lineEnd
,
point
)
{
const
l
=
getDistance
(
lineStart
,
lineEnd
)
if
(
l
===
0
)
return
getDistance
(
point
,
lineStart
)
let
t
=
((
point
[
0
]
-
lineStart
[
0
])
*
(
lineEnd
[
0
]
-
lineStart
[
0
])
+
(
point
[
1
]
-
lineStart
[
1
])
*
(
lineEnd
[
1
]
-
lineStart
[
1
]))
/
l2
t
=
c
ap01
(
t
)
return
d
istance
Squared
(
point
,
[
l
**
2
t
=
c
lamp
(
t
,
0
,
1
)
return
getD
istance
(
point
,
[
lineStart
[
0
]
+
t
*
(
lineEnd
[
0
]
-
lineStart
[
0
]),
lineStart
[
1
]
+
t
*
(
lineEnd
[
1
]
-
lineStart
[
1
]),
])
@@ -47,30 +35,30 @@ function project([x1, y1], [x2, y2], [x3, y3]) {
function
erasureInterval
(
lineStart
,
lineEnd
,
erasureCenter
,
erasureRadius
)
{
if
(
!
(
lineStart
&&
lineEnd
&&
erasureCenter
))
return
const
dist
ToSegment2
=
distToSegment
Squared
(
lineStart
,
lineEnd
,
erasureCenter
)
if
(
erasureRadius
**
2
<
dist
ToSegment2
)
return
const
dist
=
distToSegment
(
lineStart
,
lineEnd
,
erasureCenter
)
if
(
erasureRadius
<
dist
)
return
const
lineLength
=
d
istance
(
lineStart
,
lineEnd
)
const
lineLength
=
getD
istance
(
lineStart
,
lineEnd
)
if
(
lineLength
===
0
)
{
return
dist
ToSegment2
<=
erasureRadius
**
2
?
[
0
,
1
]
:
[
0
,
0
]
return
dist
<=
erasureRadius
?
[
0
,
1
]
:
[
0
,
0
]
}
const
projT
=
project
(
lineStart
,
lineEnd
,
erasureCenter
)
const
projectionPoint
=
interpolate
(
lineStart
,
lineEnd
,
projT
)
const
d2
=
d
istance
(
erasureCenter
,
projectionPoint
)
const
halfLength
=
Math
.
sqrt
(
Math
.
abs
(
sqr
(
erasureRadius
)
-
sqr
(
d2
)
))
const
d2
=
getD
istance
(
erasureCenter
,
projectionPoint
)
const
halfLength
=
Math
.
sqrt
(
Math
.
abs
(
erasureRadius
**
2
-
d2
**
2
))
if
(
halfLength
===
0
)
return
undefined
let
touchFromStartDist
=
d
istance
(
lineStart
,
projectionPoint
)
let
touchFromStartDist
=
getD
istance
(
lineStart
,
projectionPoint
)
if
(
projT
<
0
)
touchFromStartDist
=
-
touchFromStartDist
const
touchBeginFromStarDist
=
touchFromStartDist
-
halfLength
const
touchEndFromStarDist
=
touchFromStartDist
+
halfLength
return
[
c
ap01
(
touchBeginFromStarDist
/
lineLength
),
c
ap01
(
touchEndFromStarDist
/
lineLength
),
c
lamp
(
touchBeginFromStarDist
/
lineLength
,
0
,
1
),
c
lamp
(
touchEndFromStarDist
/
lineLength
,
0
,
1
),
]
}
Loading