|
| 1 | +module Dinraal |
| 2 | + def center(options = {}) |
| 3 | + x = options[:x] |
| 4 | + y = options[:y] |
| 5 | + x2 = options[:x2] |
| 6 | + y2 = options[:y2] |
| 7 | + x3 = options[:x3] |
| 8 | + y3 = options[:y3] |
| 9 | + |
| 10 | + { x: ((x + x2 + x3) / 3).to_i, y: ((y + y2 + y3) / 3).to_i } |
| 11 | + end |
| 12 | + |
| 13 | + def outline(options = {}) |
| 14 | + x = options[:x] |
| 15 | + y = options[:y] |
| 16 | + x2 = options[:x2] |
| 17 | + y2 = options[:y2] |
| 18 | + x3 = options[:x3] |
| 19 | + y3 = options[:y3] |
| 20 | + r = options[:r] |
| 21 | + g = options[:g] |
| 22 | + b = options[:b] |
| 23 | + a = options[:a] |
| 24 | + |
| 25 | + lines = [] |
| 26 | + lines << { x: x, y: y, x2: x2, y2: y2, r: r, g: g, b: b, a: a }.line! |
| 27 | + lines << { x: x3, y: y3, x2: x2, y2: y2, r: r, g: g, b: b, a: a }.line! |
| 28 | + lines << { x: x, y: y, x2: x3, y2: y3, r: r, g: g, b: b, a: a }.line! |
| 29 | + lines |
| 30 | + end |
| 31 | + |
| 32 | + def raster(options = {}) |
| 33 | + x = options[:x] |
| 34 | + y = options[:y] |
| 35 | + x2 = options[:x2] |
| 36 | + y2 = options[:y2] |
| 37 | + x3 = options[:x3] |
| 38 | + y3 = options[:y3] |
| 39 | + r = options[:r] |
| 40 | + g = options[:g] |
| 41 | + b = options[:b] |
| 42 | + a = options[:a] |
| 43 | + |
| 44 | + args = $gtk.args |
| 45 | + triangle = [[x, y], [x2, y2], [x3, y3]] |
| 46 | + triangle = triangle.sort_by { |point| point[1] } |
| 47 | + triangle = triangle.reverse |
| 48 | + |
| 49 | + line_slope = args.geometry.line_slope [triangle[0][0], triangle[0][1], triangle[2][0], triangle[2][1]] |
| 50 | + x_intercept = triangle[0][1] - (line_slope * triangle[0][0]) |
| 51 | + |
| 52 | + vertex4 = [(triangle[1][1] - x_intercept) / line_slope, triangle[1][1]] |
| 53 | + |
| 54 | + leg0 = [triangle[0], triangle[1]] |
| 55 | + leg0_slope = args.geometry.line_slope(leg0.flatten) |
| 56 | + leg0_intercept = triangle[0][1] - (leg0_slope * triangle[0][0]) |
| 57 | + |
| 58 | + leg1 = [triangle[0], vertex4] |
| 59 | + leg1_slope = args.geometry.line_slope(leg1.flatten) |
| 60 | + leg1_intercept = triangle[0][1] - (leg1_slope * triangle[0][0]) |
| 61 | + |
| 62 | + leg2 = [triangle[2], triangle[1]] |
| 63 | + leg2_slope = args.geometry.line_slope(leg2.flatten) |
| 64 | + leg2_intercept = triangle[2][1] - (leg2_slope * triangle[2][0]) |
| 65 | + |
| 66 | + leg3 = [triangle[2], vertex4] |
| 67 | + leg3_slope = args.geometry.line_slope(leg3.flatten) |
| 68 | + leg3_intercept = triangle[2][1] - (leg3_slope * triangle[2][0]) |
| 69 | + |
| 70 | + raster_lines = [] |
| 71 | + |
| 72 | + y_iter = triangle[0][1] |
| 73 | + while y_iter >= vertex4[1] |
| 74 | + raster_lines << { |
| 75 | + x: (y_iter - leg0_intercept) / leg0_slope, |
| 76 | + y: y_iter, |
| 77 | + x2: (y_iter - leg1_intercept) / leg1_slope, |
| 78 | + y2: y_iter, |
| 79 | + r: r, g: g, b: b, a: a |
| 80 | + }.line! |
| 81 | + y_iter -= 1 |
| 82 | + end |
| 83 | + |
| 84 | + y_iter = triangle[2][1] |
| 85 | + while y_iter <= vertex4[1] |
| 86 | + raster_lines << { |
| 87 | + x: (y_iter - leg2_intercept) / leg2_slope, |
| 88 | + y: y_iter, |
| 89 | + x2: (y_iter - leg3_intercept) / leg3_slope, |
| 90 | + y2: y_iter, |
| 91 | + r: r, g: g, b: b, a: a |
| 92 | + }.line! |
| 93 | + y_iter += 1 |
| 94 | + end |
| 95 | + raster_lines |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +Dinraal.extend Dinraal |
0 commit comments