motion/joybox/macros.rb in joybox-1.0.0 vs motion/joybox/macros.rb in joybox-1.1.0
- old
+ new
@@ -49,11 +49,11 @@
# Perpendicular of point, rotated 90 degrees counter-clockwise
def jbpPerp(point)
jbp(-point.y, point.x)
end
- # Perpendicular of v, rotated 90 degrees clockwise
+ # Perpendicular of point, rotated 90 degrees clockwise
def jbpRPerp(point)
jbp(point.y, -point.x)
end
# Projection of first point over second point
@@ -71,11 +71,11 @@
def jbpUnrotate(first_point, second_point)
jbp(first_point.x * second_point.x + first_point.y * second_point.y,
first_point.y * second_point.x - first_point.x * second_point.y)
end
- # Square lenght of a CGPoint
+ # Square lenght of a point
def jbpLengthSQ(point)
jbpDot(point, point)
end
# Square distance between two points
@@ -91,11 +91,11 @@
# Distance between two points
def jbpDistance(first_point, second_point)
jbpLength(jbpSub(first_point, second_point))
end
- # Point multiplied to a lenght of 1
+ # Point multiplied to a length of 1
def jbpNormalize(point)
jbpMult(point, 1.0 / jbpLength(point))
end
# Converts radians to a normalized vector
@@ -103,11 +103,11 @@
jbp(Math.cos(angle), Math.sin(angle))
end
# Converts vector to radians
def jbpToAngle(point)
- Math.atan2(v.y, v.x)
+ Math.atan2(point.y, point.x)
end
# Clamp a value between from and to
def clamp(value, minimum_inclusive, maximum_inclusive)
if minimum_inclusive > maximum_inclusive
@@ -125,11 +125,11 @@
def jbpClamp(point, minimum_inclusive, maximum_inclusive)
jbp(clamp(point.x, minimum_inclusive.x, maximum_inclusive.x),
clamp(point.y, minimum_inclusive.y, maximum_inclusive.y))
end
- # CGSize from a CGPoint
+ # CGPoint from a CGSize
def jbpFromSize(size)
jbp(size.width, size.height)
end
# Run a math operation function on each point component
@@ -161,12 +161,12 @@
# Signed angle in radians between two vector directions
def jbpAngleSigned(first_point, second_point)
first_point = jbpNormalize(first_point)
second_point = jbpNormalize(second_point)
- angle = Math.atan2(first_point.x * second_point.y - first_point.y * second_point.x),
- jbpDot(first_point, second_point)
+ angle = Math.atan2(first_point.x * second_point.y - first_point.y * second_point.x,
+ jbpDot(first_point, second_point))
angle = 0.0 if angle.abs < Float::EPSILON
angle
end
@@ -190,11 +190,11 @@
end
# Evaluates if two lines intersect
def jbpLineIntersect(point_a, point_b, point_c, point_d)
# Line undefined
- return false if point_a.x == point_b.x && point_a.y == point_b.y || point_c.x == point_d.x && point_c.y == point_d.y
+ return nil if point_a.x == point_b.x && point_a.y == point_b.y || point_c.x == point_d.x && point_c.y == point_d.y
x_b_to_a = point_b.x - point_a.x
y_b_to_a = point_b.y - point_a.y
x_d_to_c = point_d.x - point_c.x
y_d_to_c = point_d.y - point_c.y
@@ -205,30 +205,30 @@
t = x_b_to_a * y_a_to_c - y_b_to_a * x_a_to_c
denom = y_d_to_c * x_b_to_a - x_d_to_c * y_b_to_a
if denom == 0
# Lines incident
- return { s: hit_point, t: second_hit_point } unless s != 0 && t != 0
+ return { s: s, t: t } if s == 0 && t == 0
# Lines parallel and not incident
return nil
end
- { s: hit_point / denom, t: second_hit_point / denom }
+ { s: s / denom, t: t / denom }
end
# Evaluates if two segments intersect
def jbpSegmentIntersect(point_a, point_b, point_c, point_d)
r = jbpLineIntersect(point_a, point_b, point_c, point_d)
- return false unless r[:s] >= 0.0 && r[:s] <= 1.0 && r[:t] >= 0.0 && r[:t] <= 1.0
- return true
+ return false if r.nil?
+ return r[:s] >= 0.0 && r[:s] <= 1.0 && r[:t] >= 0.0 && r[:t] <= 1.0 ? true : false
end
# Intersection point between the two lines
def jbpIntersectPoint(point_a, point_b, point_c, point_d)
r = jbpLineIntersect(point_a, point_b, point_c, point_d)
return CGPointZero unless r
return CGPointMake(point_a.x + r[:s] * (point_b.x - point_a.x), point_a.y + r[:s] * (point_b.y - point_a.y))
end
end
-end
\ No newline at end of file
+end