canvas: add parameter to constructor to set background color.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
7609201c16
commit
e430e3769e
@ -44,7 +44,8 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
let w = 800;
|
let w = 800;
|
||||||
let h = 600;
|
let h = 600;
|
||||||
let mut c = Canvas::new(w, h);
|
let bg = Color::new(0.2, 0.2, 0.2);
|
||||||
|
let mut c = Canvas::new(w, h, bg);
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let w = w as f32;
|
let w = w as f32;
|
||||||
let h = h as f32;
|
let h = h as f32;
|
||||||
|
|||||||
@ -23,7 +23,8 @@ fn draw_dot(c: &mut Canvas, x: usize, y: usize) {
|
|||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let w = 200;
|
let w = 200;
|
||||||
let h = w;
|
let h = w;
|
||||||
let mut c = Canvas::new(w, h);
|
let bg = Color::new(0.2, 0.2, 0.2);
|
||||||
|
let mut c = Canvas::new(w, h, bg);
|
||||||
let t = Matrix4x4::translation(0., 0.4, 0.);
|
let t = Matrix4x4::translation(0., 0.4, 0.);
|
||||||
let p = Tuple::point(0., 0., 0.);
|
let p = Tuple::point(0., 0., 0.);
|
||||||
let rot_hour = Matrix4x4::rotation_z(-PI / 6.);
|
let rot_hour = Matrix4x4::rotation_z(-PI / 6.);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ use anyhow::Result;
|
|||||||
|
|
||||||
use rtchallenge::{
|
use rtchallenge::{
|
||||||
canvas::Canvas,
|
canvas::Canvas,
|
||||||
|
matrices::Matrix4x4,
|
||||||
rays::Ray,
|
rays::Ray,
|
||||||
spheres::{intersect, Sphere},
|
spheres::{intersect, Sphere},
|
||||||
tuples::{Color, Tuple},
|
tuples::{Color, Tuple},
|
||||||
@ -12,14 +13,17 @@ use rtchallenge::{
|
|||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let w = 200;
|
let w = 200;
|
||||||
let h = w;
|
let h = w;
|
||||||
let mut c = Canvas::new(w, h);
|
let bg = Color::new(0.2, 0.2, 0.2);
|
||||||
|
let mut c = Canvas::new(w, h, bg);
|
||||||
let ray_origin = Tuple::point(0., 0., -5.);
|
let ray_origin = Tuple::point(0., 0., -5.);
|
||||||
let wall_z = 10.;
|
let wall_z = 10.;
|
||||||
let wall_size = 7.;
|
let wall_size = 7.;
|
||||||
let pixel_size = wall_size / w as f32;
|
let pixel_size = wall_size / w as f32;
|
||||||
let half = wall_size / 2.;
|
let half = wall_size / 2.;
|
||||||
let color = Color::new(1., 0., 0.);
|
let color = Color::new(1., 0., 0.);
|
||||||
let shape = Sphere::default();
|
let mut shape = Sphere::default();
|
||||||
|
shape.transform =
|
||||||
|
Matrix4x4::shearing(1., 0., 0., 0., 0., 0.) * Matrix4x4::scaling(0.5, 1., 1.0);
|
||||||
|
|
||||||
for y in 0..h {
|
for y in 0..h {
|
||||||
let world_y = half - pixel_size * y as f32;
|
let world_y = half - pixel_size * y as f32;
|
||||||
|
|||||||
@ -21,8 +21,8 @@ pub struct Canvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
pub fn new(width: usize, height: usize) -> Canvas {
|
pub fn new(width: usize, height: usize, background: Color) -> Canvas {
|
||||||
let pixels = vec![Color::new(1., 0., 1.); width * height];
|
let pixels = vec![background; width * height];
|
||||||
Canvas {
|
Canvas {
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
@ -78,7 +78,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_canvas() {
|
fn create_canvas() {
|
||||||
let c = Canvas::new(10, 20);
|
let bg = Color::new(0.0, 0.0, 0.0);
|
||||||
|
let c = Canvas::new(10, 20, bg);
|
||||||
assert_eq!(c.width, 10);
|
assert_eq!(c.width, 10);
|
||||||
assert_eq!(c.height, 20);
|
assert_eq!(c.height, 20);
|
||||||
let black = Color::new(0., 0., 0.);
|
let black = Color::new(0., 0., 0.);
|
||||||
@ -89,7 +90,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn write_to_canvas() {
|
fn write_to_canvas() {
|
||||||
let mut c = Canvas::new(10, 20);
|
let bg = Color::new(0.2, 0.2, 0.2);
|
||||||
|
let mut c = Canvas::new(10, 20, bg);
|
||||||
let red = Color::new(1., 0., 0.);
|
let red = Color::new(1., 0., 0.);
|
||||||
c.set(2, 3, red);
|
c.set(2, 3, red);
|
||||||
assert_eq!(c.get(2, 3), red);
|
assert_eq!(c.get(2, 3), red);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user