Minor updates, add line_intersect example

This commit is contained in:
Alex Clink 2024-10-23 00:31:30 -04:00
parent 615cdc5d89
commit effbf83aa3
No known key found for this signature in database
GPG key ID: 081CC9920757FABF
8 changed files with 87 additions and 19 deletions

1
.gitignore vendored
View file

@ -7,4 +7,5 @@
# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
/shard.override.yml
/examples/build/*

View file

@ -1,7 +1,6 @@
require "pf3d"
require "../src/game"
require "../src/controller"
require "../src/sprite"
require "../src/pixel"
require "../src/sprite"

View file

@ -30,7 +30,7 @@ class CubicBezier < PF::Game
if point = @selected_point
point.value = cursor.to_f
else
@hover_point = @curve.points.find { |p| cursor.distance(p.value) < 4 }
@hover_point = @curve.point_pointers.find { |p| cursor.distance(p.value) < 4 }
end
end
@ -50,32 +50,34 @@ class CubicBezier < PF::Game
def draw
clear
draw_line(0, height // 2, width, height // 2, PF::Pixel.new(25, 25, 25))
draw_line(@curve.p0, @curve.p1, CTL_COLOR)
draw_line(@curve.p3, @curve.p2, CTL_COLOR)
draw_string("Length: " + @curve.length.round(2).to_s, 5, 5, @font, FONT_COLOR)
draw_rect(*@curve.rect.map(&.to_i), CTL_COLOR)
draw_rect(@curve.rect, CTL_COLOR)
draw_curve(@curve, CURVE_COLOR)
fill_circle(@curve.p0.to_i, 2, POINT_COLOR)
fill_circle(@curve.p1.to_i, 2, POINT_COLOR)
fill_circle(@curve.p2.to_i, 2, POINT_COLOR)
fill_circle(@curve.p3.to_i, 2, POINT_COLOR)
@curve.horizontal_intersects(height // 2) do |p|
draw_circle(p.to_i, 3, PF::Pixel::Orange)
end
draw_string("P1 (#{@curve.p0.x.to_i}, #{@curve.p0.y.to_i})", @curve.p0, @font, FONT_COLOR)
draw_string("P2 (#{@curve.p1.x.to_i}, #{@curve.p1.y.to_i})", @curve.p1, @font, FONT_COLOR)
draw_string("P3 (#{@curve.p2.x.to_i}, #{@curve.p2.y.to_i})", @curve.p2, @font, FONT_COLOR)
draw_string("P4 (#{@curve.p3.x.to_i}, #{@curve.p3.y.to_i})", @curve.p3, @font, FONT_COLOR)
@curve.points.each do |p|
fill_circle(p.to_i, 2, POINT_COLOR)
end
@curve.points.each_with_index do |p, i|
draw_string("P#{i} (#{p.x.to_i}, #{p.y.to_i})", p, @font, FONT_COLOR)
end
if point = @hover_point
draw_circle(point.value.to_i, 5, SEL_COLOR)
end
@curve.extremities.each do |point|
point.try do |p|
draw_circle(p.to_i, 3, EXT_Y_COLOR)
end
@curve.extrema do |point|
draw_circle(point.to_i, 3, EXT_Y_COLOR)
end
end
end

View file

@ -0,0 +1,62 @@
require "../src/game"
class LineIntersect < PF::Game
include PF2d
@line_1 : Line(Vec2(Float64))
@line_2 : Line(Vec2(Float64))
@hover_point : Vec2(Float64)*? = nil
@selected_point : Vec2(Float64)*? = nil
@font = Pixelfont::Font.new("#{__DIR__}/../lib/pixelfont/fonts/pixel-5x7.txt")
def initialize(*args, **kwargs)
super
@line_1 = Line[Vec[5, 5].to_f64, Vec[width - 5, height - 5].to_f64]
@line_2 = Line[Vec[width - 5, 5].to_f64, Vec[5, height - 5].to_f64]
end
def on_mouse_motion(cursor)
if point = @selected_point
point.value = cursor.to_f
else
@hover_point = {*@line_1.point_pointers, *@line_2.point_pointers}.find do |point|
point.value.distance(cursor) < 3
end
end
end
def on_mouse_button(event)
if event.button == 1
if event.pressed?
@selected_point = @hover_point
else
@selected_point = nil
end
end
end
def update(dt)
end
def draw
clear
draw_line(@line_1, PF::Pixel::Yellow)
draw_line(@line_2, PF::Pixel::Yellow)
fill_circle(@line_1.p1.to_i, 3, PF::Pixel::Red)
fill_circle(@line_1.p2.to_i, 3, PF::Pixel::Red)
fill_circle(@line_2.p1.to_i, 3, PF::Pixel::Red)
fill_circle(@line_2.p2.to_i, 3, PF::Pixel::Red)
if point = @hover_point
draw_circle(point.value.to_i, 5, PF::Pixel::Blue)
end
if point = @line_1.intersects?(@line_2)
fill_circle(point.to_i, 3, PF::Pixel::Green)
end
end
end
LineIntersect.new(300, 300, 2).run!

View file

@ -1,5 +1,5 @@
name: pixelfaucet
version: 0.0.8
version: 0.0.9
authors:
- Alex Clink <alexclink@gmail.com>
@ -14,7 +14,10 @@ dependencies:
version: 0.1.0
pf2d:
github: SleepingInsomniac/pf2d
version: 0.3.0
pf3d:
github: SleepingInsomniac/pf3d
version: 0.1.1
pixelfont:
github: SleepingInsomniac/pixelfont
version: 1.1.1

View file

@ -25,7 +25,7 @@ module PF
sound.release!(time)
spawn do
sleep @envelope.release.duration
sleep @envelope.release.duration.seconds
@sounds.delete(sound)
@notes.delete(note_id)
end

View file

@ -1,5 +1,3 @@
require "./lib_sdl"
module PF
alias Keys = LibSDL::Scancode
alias KeyCodes = LibSDL::Keycode

View file

@ -51,7 +51,10 @@ module PF
end
end
# Could be called multiple times per frame, used for updating the simulation
abstract def update(dt : Float64)
# Called to draw the current frame
abstract def draw
# Returns the width of the window and render area