Skip to content

Commit b182f3c

Browse files
committed
fixing jarvis code for julia
1 parent 3176e82 commit b182f3c

File tree

1 file changed

+18
-62
lines changed
  • chapters/computational_geometry/gift_wrapping/jarvis_march/code/julia

1 file changed

+18
-62
lines changed

chapters/computational_geometry/gift_wrapping/jarvis_march/code/julia/jarvis.jl

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
1-
struct Point
1+
struct Pos
22
x::Float64
33
y::Float64
44
end
55

6-
function dist(point1::Point, point2::Point)
7-
return sqrt((point1.x - point2.x)^2 + (point1.y - point2.y)^2)
6+
function jarvis_cross(point1::Pos, point2::Pos, point3::Pos)
7+
vec1 = Pos(point2.x - point1.x, point2.y - point1.y)
8+
vec2 = Pos(point3.x - point2.x, point3.y - point2.y)
9+
ret_cross = vec1.x*vec2.y - vec1.y*vec2.x
10+
return ret_cross*ret_cross
811
end
912

10-
function jarvis_angle(point1::Point, point2::Point, point3::Point)
11-
#=
12-
# Find distances between all points
13-
a = dist(point3, point2)
14-
b = dist(point3, point1)
15-
c = dist(point1, point2)
16-
17-
println(point1)
18-
println(point2)
19-
println(point3)
20-
println(a)
21-
println(b)
22-
println(c)
23-
println(-(c*c - a*a - b*b)/(2*a*b))
24-
println()
25-
ret_angle = acos((b*b - a*a - c*c)/(2*a*c))
26-
println(ret_angle)
27-
println()
28-
29-
if(sign(point1.x - point2.x) != sign(point1.x - point3.x))
30-
ret_angle += 0.5*pi
31-
end
32-
33-
if (isnan(ret_angle))
34-
exit(1)
35-
end
36-
37-
return ret_angle
38-
=#
39-
return (point1.x - point2.x)*(point3.x-point2.x) +
40-
(point1.y - point2.y)*(point3.x-point2.y)
41-
end
42-
43-
function jarvis_march(points::Vector{Point})
44-
hull = Vector{Point}()
13+
function jarvis_march(points::Vector{Pos})
14+
hull = Vector{Pos}()
4515

4616
# sorting array based on leftmost point
4717
sort!(points, by = item -> item.x)
@@ -50,51 +20,37 @@ function jarvis_march(points::Vector{Point})
5020
i = 1
5121
curr_point = points[2]
5222

53-
threshold = 1e-5
54-
55-
# Find angle between points
56-
curr_product = 0.0
23+
# Find cross product between points
24+
curr_product = jarvis_cross(Pos(0,0), hull[1], curr_point)
5725
#while (curr_point != hull[1])
5826
while (length(hull) < 4)
59-
println(hull)
6027
for point in points
61-
product = 0.0
28+
product = 0.0
6229
if (i == 1)
6330
if (hull[i] != point)
64-
product = hull[i].x*(point.x - hull[i].x)+
65-
hull[i].y*(point.y - hull[i].y)
66-
67-
println(product)
68-
end
31+
product = jarvis_cross(Pos(0,0), hull[i], point)
32+
end
6933
else
7034
if (hull[i] != point && hull[i-1] != point)
71-
product = (hull[i].x - hull[i-1].x)*(point.x - hull[i].x)+
72-
(hull[i].y - hull[i-1].y)*(point.y - hull[i].y)
35+
product = jarvis_cross(hull[i-1], hull[i], point)
7336
end
7437
end
75-
76-
println(hull[i])
77-
println(point)
78-
println(product)
79-
println(curr_product)
80-
println()
81-
if (product < curr_product || curr_product == 0)
82-
println(product, '\t', curr_product)
38+
if (product > curr_product)
8339
curr_point = point
8440
curr_product = product
8541
end
8642
end
8743
push!(hull, curr_point)
88-
i += 1
8944
curr_product = 0
45+
i += 1
9046
end
9147

92-
9348
return hull
9449
end
9550

9651
function main()
97-
points = [Point(2,1.9), Point(1, 1), Point(2, 4), Point(3, 1)]
52+
53+
points = [Pos(2,1.5), Pos(1, 1), Pos(2, 4), Pos(3, 1)]
9854
hull = jarvis_march(points)
9955
println(hull)
10056
end

0 commit comments

Comments
 (0)