Module: Rough::PointsOnCurve

Defined in:
lib/rough/points_on_curve.rb

Class Method Summary collapse

Class Method Details

.curve_to_bezier(points_in, curve_tightness = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rough/points_on_curve.rb', line 25

def curve_to_bezier(points_in, curve_tightness = 0)
  len = points_in.length
  raise "A curve must have at least three points." if len < 3

  out = []
  if len == 3
    out.push(points_in[0].dup, points_in[1].dup, points_in[2].dup, points_in[2].dup)
  else
    points = []
    points.push(points_in[0], points_in[0])
    (1...points_in.length).each do |i|
      points.push(points_in[i])
      points.push(points_in[i]) if i == points_in.length - 1
    end

    s = 1 - curve_tightness
    out.push(points[0].dup)
    i = 1
    while (i + 2) < points.length
      cached = points[i]
      b1 = [
        cached[0] + (s * points[i + 1][0] - s * points[i - 1][0]) / 6.0,
        cached[1] + (s * points[i + 1][1] - s * points[i - 1][1]) / 6.0
      ]
      b2 = [
        points[i + 1][0] + (s * points[i][0] - s * points[i + 2][0]) / 6.0,
        points[i + 1][1] + (s * points[i][1] - s * points[i + 2][1]) / 6.0
      ]
      b3 = [points[i + 1][0], points[i + 1][1]]
      out.push(b1, b2, b3)
      i += 1
    end
  end
  out
end

.points_on_bezier_curves(points, tolerance = 0.15, distance = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rough/points_on_curve.rb', line 7

def points_on_bezier_curves(points, tolerance = 0.15, distance = nil)
  new_points = []
  num_segments = (points.length - 1) / 3
  num_segments.times do |i|
    offset = i * 3
    get_points_on_bezier_curve_with_splitting(points, offset, tolerance, new_points)
  end
  if distance && distance > 0
    simplify_points(new_points, 0, new_points.length, distance)
  else
    new_points
  end
end

.simplify(points, distance) ⇒ Object



21
22
23
# File 'lib/rough/points_on_curve.rb', line 21

def simplify(points, distance)
  simplify_points(points, 0, points.length, distance)
end