Sample-generic code!
This commit is contained in:
parent
43eb0a2ea8
commit
f2cbe6413a
|
|
@ -20,25 +20,7 @@ pub fn main() {
|
||||||
let canvas = create_drawing_window("Refraction");
|
let canvas = create_drawing_window("Refraction");
|
||||||
canvas.draw(|gc| {
|
canvas.draw(|gc| {
|
||||||
gc.canvas_height(1000.0);
|
gc.canvas_height(1000.0);
|
||||||
|
space.render(gc);
|
||||||
gc.new_path();
|
|
||||||
//gc.circle(0.0, 0.0, space.coil_r + space.coil_w + space.coil_m);
|
|
||||||
//gc.circle(0.0, 0.0, space.coil_r + space.coil_w);
|
|
||||||
//gc.circle(0.0, 0.0, space.coil_r - space.coil_w);
|
|
||||||
//gc.circle(0.0, 0.0, space.coil_r - space.coil_w - space.coil_m);
|
|
||||||
gc.winding_rule(WindingRule::EvenOdd);
|
|
||||||
gc.fill_color(Color::Rgba(0.8, 0.8, 0.8, 1.0));
|
|
||||||
gc.fill();
|
|
||||||
|
|
||||||
gc.line_width(0.5);
|
|
||||||
gc.stroke_color(Color::Rgba(1.0, 0.5, 0.0, 1.0));
|
|
||||||
for y in itertools_num::linspace(-1.0, 1.0, 101) {
|
|
||||||
draw_ray(gc, &space, vec2(-500.0, 0.0), vec2(1.0, y));
|
|
||||||
}
|
|
||||||
// gc.stroke_color(Color::Rgba(0.0, 0.5, 1.0, 1.0));
|
|
||||||
// for y in itertools_num::linspace(-1.0, 1.0, 101) {
|
|
||||||
// draw_ray(gc, &space, vec2(0.0, space.coil_r), vec2(1.0, y));
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -56,6 +38,52 @@ fn draw_ray(gc: &mut Vec<Draw>, space: &impl Metric, base: Vec2, dir: Vec2) {
|
||||||
gc.stroke();
|
gc.stroke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn draw_fan(gc: &mut Vec<Draw>, space: &impl Metric, base: Vec2, dir: Vec2, spread: f32) {
|
||||||
|
let dir = dir.normalize();
|
||||||
|
let v = vec2(-dir.y, dir.x);
|
||||||
|
for y in itertools_num::linspace(-spread, spread, 101) {
|
||||||
|
draw_ray(gc, space, base, dir + y * v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Renderable {
|
||||||
|
fn render(&self, gc: &mut Vec<Draw>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Renderable for Coil {
|
||||||
|
fn render(&self, gc: &mut Vec<Draw>) {
|
||||||
|
gc.new_path();
|
||||||
|
gc.circle(0.0, 0.0, self.coil_r + self.coil_w + self.coil_m);
|
||||||
|
gc.circle(0.0, 0.0, self.coil_r + self.coil_w);
|
||||||
|
gc.circle(0.0, 0.0, self.coil_r - self.coil_w);
|
||||||
|
gc.circle(0.0, 0.0, self.coil_r - self.coil_w - self.coil_m);
|
||||||
|
gc.winding_rule(WindingRule::EvenOdd);
|
||||||
|
gc.fill_color(Color::Rgba(0.8, 0.8, 0.8, 1.0));
|
||||||
|
gc.fill();
|
||||||
|
gc.line_width(0.5);
|
||||||
|
gc.stroke_color(Color::Rgba(1.0, 0.5, 0.0, 1.0));
|
||||||
|
draw_fan(gc, self, vec2(-500.0, 0.0), vec2(1.0, 0.0), 1.0);
|
||||||
|
gc.stroke_color(Color::Rgba(0.0, 0.5, 1.0, 1.0));
|
||||||
|
draw_fan(gc, self, vec2(0.0, self.coil_r), vec2(1.0, 0.0), 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Renderable for Rect {
|
||||||
|
fn render(&self, gc: &mut Vec<Draw>) {
|
||||||
|
gc.new_path();
|
||||||
|
gc.rect(self.box_a.x - self.box_m.x, self.box_a.y - self.box_m.y, self.box_b.x + self.box_m.x, self.box_b.y + self.box_m.y);
|
||||||
|
gc.rect(self.box_a.x, self.box_a.y, self.box_b.x, self.box_b.y);
|
||||||
|
gc.winding_rule(WindingRule::EvenOdd);
|
||||||
|
gc.fill_color(Color::Rgba(0.8, 0.8, 0.8, 1.0));
|
||||||
|
gc.fill();
|
||||||
|
gc.line_width(0.5);
|
||||||
|
gc.stroke_color(Color::Rgba(1.0, 0.5, 0.0, 1.0));
|
||||||
|
draw_fan(gc, self, vec2(-500.0, 0.0), vec2(1.0, 0.0), 1.0);
|
||||||
|
gc.stroke_color(Color::Rgba(0.0, 0.5, 1.0, 1.0));
|
||||||
|
draw_fan(gc, self, vec2(0.0, 0.5 * self.box_a.y), vec2(1.0, 1.0), 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Coil {
|
struct Coil {
|
||||||
coil_m: f32,
|
coil_m: f32,
|
||||||
coil_scale: f32,
|
coil_scale: f32,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user