Skip to content

Instantly share code, notes, and snippets.

@quietsamurai98
Created October 3, 2020 05:31
Show Gist options
  • Save quietsamurai98/0110e4411ad64fd1d37fcc7cf2ec442e to your computer and use it in GitHub Desktop.
Save quietsamurai98/0110e4411ad64fd1d37fcc7cf2ec442e to your computer and use it in GitHub Desktop.
Draws any triangle using only two sprites
# @return [Array<Hash>]
# @param [Float] x1
# @param [Float] y1
# @param [Float] x2
# @param [Float] y2
# @param [Float] x3
# @param [Float] y3
# @param [Float] r
# @param [Float] g
# @param [Float] b
# @param [Float] a
def draw_tri(x1, y1, x2, y2, x3, y3, r = 0, g = 0, b = 0, a = 255)
v1, v2, v3 = [x1, y1], [x2, y2], [x3, y3]
v2, v3 = v3, v2 if (v2.x - v1.x) * (v3.y - v1.y) - (v2.y - v1.y) * (v3.x - v1.x) < 0
c_i = [0, 1, 2].max_by { |i| ([v1, v2, v3][i - 1].x - [v1, v2, v3][i - 2].x) ** 2 + ([v1, v2, v3][i - 1].y - [v1, v2, v3][i - 2].y) ** 2 }
vc, vb, va = [v1, v2, v3].rotate(c_i)
b_a = [vb.x - va.x, vb.y - va.y]
c_a = [vc.x - va.x, vc.y - va.y]
scale = (c_a.x * b_a.x + c_a.y * b_a.y) / (b_a.x * b_a.x + b_a.y * b_a.y)
d_a = [b_a.x * scale, b_a.y * scale]
d = [d_a.x + va.x, d_a.y + va.y]
h = Math.sqrt((d_a.x - b_a.x) * (d_a.x - b_a.x) + (d_a.y - b_a.y) * (d_a.y - b_a.y))
w = Math.sqrt((d.x - vc.x) * (d.x - vc.x) + (d.y - vc.y) * (d.y - vc.y))
wh = Math.sqrt((d.x - va.x) * (d.x - va.x) + (d.y - va.y) * (d.y - va.y))
angle1 = Math.atan2(vc.y - d.y, vc.x - d.x).to_degrees + 360
angle2 = Math.atan2(va.y - d.y, va.x - d.x).to_degrees + 360
[
{
x: d.x,
y: d.y,
w: w,
h: h,
path: 'sprites/triangle1.png', # Right triangle sprite. Lower right white, upper left transparent.
angle_anchor_x: 0,
angle_anchor_y: 0,
angle: angle1,
r: r,
g: g,
b: b,
a: a
}.sprite,
{
x: d.x,
y: d.y,
w: wh,
h: w,
path: 'sprites/triangle2.png', # Right triangle sprite. Lower left white, upper right transparent.
angle_anchor_x: 0,
angle_anchor_y: 0,
angle: angle2,
r: r,
g: g,
b: b,
a: a
}.sprite
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment