Class: PerfectShape::CubicBezierCurve

Inherits:
Shape
  • Object
show all
Includes:
MultiPoint
Defined in:
lib/perfect_shape/cubic_bezier_curve.rb

Overview

Instance Attribute Summary

Attributes included from MultiPoint

#points

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MultiPoint

#initialize, #max_x, #max_y, #min_x, #min_y

Methods inherited from Shape

#==, #bounding_box, #center_x, #center_y, #height, #max_x, #max_y, #min_x, #min_y, #normalize_point, #width

Class Method Details

.point_crossings(x1, y1, xc1, yc1, xc2, yc2, x2, y2, px, py, level = 0) ⇒ Object

Calculates the number of times the cubic bézier curve from (x1,y1) to (x2,y2) crosses the ray extending to the right from (x,y). If the point lies on a part of the curve, then no crossings are counted for that intersection. the level parameter should be 0 at the top-level call and will count up for each recursion level to prevent infinite recursion +1 is added for each crossing where the Y coordinate is increasing -1 is added for each crossing where the Y coordinate is decreasing



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/perfect_shape/cubic_bezier_curve.rb', line 37

def point_crossings(x1, y1, xc1, yc1, xc2, yc2, x2, y2, px, py, level = 0)
  return 0 if (py <  y1 && py <  yc1 && py <  yc2 && py <  y2)
  return 0 if (py >= y1 && py >= yc1 && py >= yc2 && py >= y2)
  # Note y1 could equal yc1...
  return 0 if (px >= x1 && px >= xc1 && px >= xc2 && px >= x2)
  if (px <  x1 && px <  xc1 && px <  xc2 && px <  x2)
    if (py >= y1)
      return 1 if (py < y2)
    else
      # py < y1
      return -1 if (py >= y2)
    end
    # py outside of y12 range, and/or y1==yc1
    return 0
  end
  # double precision only has 52 bits of mantissa
  return PerfectShape::Line.point_crossings(x1, y1, x2, y2, px, py) if (level > 52)
  xmid = BigDecimal((xc1 + xc2).to_s) / 2;
  ymid = BigDecimal((yc1 + yc2).to_s) / 2;
  xc1 = BigDecimal((x1 + xc1).to_s) / 2;
  yc1 = BigDecimal((y1 + yc1).to_s) / 2;
  xc2 = BigDecimal((xc2 + x2).to_s) / 2;
  yc2 = BigDecimal((yc2 + y2).to_s) / 2;
  xc1m = BigDecimal((xc1 + xmid).to_s) / 2;
  yc1m = BigDecimal((yc1 + ymid).to_s) / 2;
  xmc1 = BigDecimal((xmid + xc2).to_s) / 2;
  ymc1 = BigDecimal((ymid + yc2).to_s) / 2;
  xmid = BigDecimal((xc1m + xmc1).to_s) / 2;
  ymid = BigDecimal((yc1m + ymc1).to_s) / 2;
  # [xy]mid are NaN if any of [xy]c0m or [xy]mc1 are NaN
  # [xy]c0m or [xy]mc1 are NaN if any of [xy][c][01] are NaN
  # These values are also NaN if opposing infinities are added
  return 0 if (xmid.nan? || ymid.nan?)
  point_crossings(x1, y1, xc1, yc1, xc1m, yc1m, xmid, ymid, px, py, level+1) +
    point_crossings(xmid, ymid, xmc1, ymc1, xc2, yc2, x2, y2, px, py, level+1)
end

Instance Method Details

#contain?(x_or_point, y = nil) ⇒ @code true

Checks if cubic bézier curve contains point (two-number Array or x, y args)

the cubic bézier curve, false if the point lies outside of the cubic bézier curve’s bounds.

Parameters:

  • x

    The X coordinate of the point to test.

  • y (defaults to: nil)

    The Y coordinate of the point to test.

Returns:

  • (@code true)

    if the point lies within the bound of



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/perfect_shape/cubic_bezier_curve.rb', line 86

def contain?(x_or_point, y = nil)
  x, y = normalize_point(x_or_point, y)
  return unless x && y
  
  # Either x or y was infinite or NaN.
  # A NaN always produces a negative response to any test
  # and Infinity values cannot be "inside" any path so
  # they should return false as well.
  return false if (!(x * 0.0 + y * 0.0 == 0.0))
  # We count the "Y" crossings to determine if the point is
  # inside the curve bounded by its closing line.
  x1 = points[0][0]
  y1 = points[0][1]
  x2 = points[3][0]
  y2 = points[3][1]
  line = PerfectShape::Line.new(points: [[x1, y1], [x2, y2]])
  crossings = line.point_crossings(x, y) + point_crossings(x, y);
  (crossings & 1) == 1
end

#point_crossings(x_or_point, y = nil, level = 0) ⇒ Object

Calculates the number of times the cubic bézier curve crosses the ray extending to the right from (x,y). If the point lies on a part of the curve, then no crossings are counted for that intersection. the level parameter should be 0 at the top-level call and will count up for each recursion level to prevent infinite recursion +1 is added for each crossing where the Y coordinate is increasing -1 is added for each crossing where the Y coordinate is decreasing



114
115
116
117
118
# File 'lib/perfect_shape/cubic_bezier_curve.rb', line 114

def point_crossings(x_or_point, y = nil, level = 0)
  x, y = normalize_point(x_or_point, y)
  return unless x && y
  CubicBezierCurve.point_crossings(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1], x, y, level)
end