File: path.rb

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module Cairo
  class Context
    module Path
      def transform_path(path, &block)
        transformed_path = Cairo::Path.new
        path.each do |type, points|
          case type
          when PATH_MOVE_TO
            transformed_path.move_to(*points.collect(&block).flatten)
          when PATH_LINE_TO
            transformed_path.line_to(*points.collect(&block).flatten)
          when PATH_CURVE_TO
            transformed_path.curve_to(*points.collect(&block).flatten)
          when PATH_CLOSE_PATH
            transformed_path.close
          end
        end
        transformed_path
      end

      def map_path_onto(path)
        parameterized_path = parameterize_path(path)
        transformed_path = transform_path(copy_path) do |x, y|
          current_point = nil
          d = x
          i = -1
          type = points = nil
          path.each do |_type, _points|
            type, points = _type, _points
            i += 1
            break if d < parameterized_path[i]
            d -= parameterized_path[i]
            case type
            when PATH_MOVE_TO
              current_point = points[0]
            when PATH_LINE_TO
              current_point = points[0]
            when PATH_CURVE_TO
              current_point = points[2]
            when PATH_CLOSE_PATH
            end
          end

          case type
          when PATH_MOVE_TO
            [x, y]
          when PATH_LINE_TO
            ratio = d / parameterized_path[i]
            current_x, current_y = current_point
            lx, ly = points[0]
            new_x = current_x * (1 - ratio) + lx * ratio
            new_y = current_y * (1 - ratio) + ly * ratio
            dx = -(current_x - lx)
            dy = -(current_y - ly)

            ratio = y / parameterized_path[i]
            [new_x + -dy * ratio, new_y + dx * ratio]
          when PATH_CURVE_TO
            ratio = d / parameterized_path[i]
            current_x, current_y = current_point
            cx0, cy0 = points[0]
            cx1, cy1 = points[1]
            cx2, cy2 = points[2]

            new_x = current_x * (1 - ratio) * (1 - ratio) * (1 - ratio) +
                    3 * cx0   * (1 - ratio) * (1 - ratio) * ratio +
                    3 * cx1   * (1 - ratio) *      ratio  * ratio +
                        cx2   *      ratio  *      ratio  * ratio
            new_y = current_y * (1 - ratio) * (1 - ratio) * (1 - ratio) +
                    3 * cy0   * (1 - ratio) * (1 - ratio) * ratio +
                    3 * cy1   * (1 - ratio) *      ratio  * ratio +
                        cy2   *      ratio  *      ratio  * ratio

            dx = -3 * current_x * (1 - ratio) * (1 - ratio) +
                  3 * cx0       * (1 - 4 * ratio + 3 * ratio * ratio) +
                  3 * cx1       * (    2 * ratio - 3 * ratio * ratio) +
                  3 * cx2       *      ratio  * ratio
            dy = -3 * current_y * (1 - ratio) * (1 - ratio) +
                  3 * cy0       * (1 - 4 * ratio + 3 * ratio * ratio) +
                  3 * cy1       * (    2 * ratio - 3 * ratio * ratio) +
                  3 * cy2       *      ratio  * ratio

            ratio = y / Math.sqrt(dx ** 2 + dy ** 2)

            [new_x + -dy * ratio, new_y + dx * ratio]
          when PATH_CLOSE_PATH
            [x, y]
          end
        end
        new_path
        append_path(transformed_path)
      end

      private
      def parameterize_path(path)
        current_point = nil
        path.collect do |type, points|
          result = 0
          case type
          when PATH_MOVE_TO
            current_point = points[0]
          when PATH_LINE_TO
            result = current_point.distance(points[0])
            current_point = points[0]
          when PATH_CURVE_TO
            result = current_point.distance(points[0])
            result += points[0].distance(points[1])
            result += points[1].distance(points[2])
            current_point = points[2]
          when PATH_CLOSE_PATH
          end
          result
        end
      end
    end
  end
end