Add more methods to PF::Line

This commit is contained in:
Alex Clink 2021-12-30 18:18:11 -05:00
parent 3bb6547bf7
commit 098d285fa8
4 changed files with 56 additions and 2 deletions

View file

@ -3,7 +3,7 @@ require "./lib_sdl"
require "./pixel"
require "./point"
require "./controller"
require "./game/fill_triangle"
require "./game/*"
module PF
abstract class Game

View file

@ -33,6 +33,7 @@ module PF
0.upto(height) do |y|
if slope_left == 0
# When there is no rise, set the x value directly
x_left = line_left.p2.x
else
x_left = ((y - (line_left.p1.y - p1.y)) / slope_left).round.to_i + line_left.p1.x

View file

@ -1,4 +1,8 @@
require "crystaledge/vector2"
module PF
include CrystalEdge
struct Line(T)
property p1 : Point(T), p2 : Point(T)
@ -18,8 +22,53 @@ module PF
rise / run
end
def inv_slope
return 0.0 if rise == 0
run / rise
end
def left
@p1.x < @p2.x ? @p1.x : @p2.x
end
def right
@p1.x > @p2.x ? @p1.x : @p2.x
end
def top
@p1.y > @p2.y ? @p2.y : @p1.y
end
def bottom
@p1.y > @p2.y ? @p1.y : @p2.y
end
def contains_y?(y)
if @p1.y < @p2.y
top, bottom = @p1.y, @p2.y
else
top, bottom = @p2.y, @p1.y
end
y >= top && y <= bottom
end
def y_at(x)
return p1.y if slope == 1.0
x * slope + p1.y
end
def x_at(y)
return p1.x if slope == 0.0
(y - p1.y) / slope + p1.x
end
def length
Math.sqrt((run * 2) + (rise * 2))
Math.sqrt((run.abs * 2) + (rise.abs * 2))
end
def to_vector
Vector2.new(run.to_f64, rise.to_f64)
end
end
end

View file

@ -8,6 +8,10 @@ module PF
new(255, 255, 255)
end
def self.black
new(0, 0, 0)
end
def self.red
new(255, 0, 0)
end