Trait pixel_engine_draw::traits::ShapesTrait
source · pub trait ShapesTrait: SmartDrawingTrait {
fn draw_text<P: Into<Vi2d>>(
&mut self,
pos: P,
scale: u32,
col: Color,
text: &str
) { ... }
fn draw_line<P: Into<Vi2d>>(&mut self, p1: P, p2: P, col: Color) { ... }
fn draw_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color) { ... }
fn fill_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color) { ... }
fn draw_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color) { ... }
fn fill_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color) { ... }
fn draw_triangle<P: Into<Vi2d>>(
&mut self,
pts1: P,
pts2: P,
pts3: P,
col: Color
) { ... }
fn fill_triangle<P: Into<Vi2d>>(
&mut self,
pts1: P,
pts2: P,
pts3: P,
col: Color
) { ... }
}
Expand description
A trait that regroups all the Shapes Drawing
You don’t need to implement anything other that DrawSpriteTrait
to use it
Provided Methods§
sourcefn draw_text<P: Into<Vi2d>>(&mut self, pos: P, scale: u32, col: Color, text: &str)
fn draw_text<P: Into<Vi2d>>(&mut self, pos: P, scale: u32, col: Color, text: &str)
Draw text to the screen
scale
must be >= 1
The textsize will be equal to scale * 8
for the height and scale * 8 * text.len()
for
the width
This will handle \n
treating it as a new line, but wont do any newline stuff if it is
drawing out of the screen
sourcefn draw_line<P: Into<Vi2d>>(&mut self, p1: P, p2: P, col: Color)
fn draw_line<P: Into<Vi2d>>(&mut self, p1: P, p2: P, col: Color)
Draw a line between two points, You don’t need to do anything with the points for it to work, it will swap them it needed.
Examples found in repository?
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
fn draw_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color) {
let Vi2d { x, y } = pos.into();
let Vi2d { x: w, y: h } = size.into() - Vi2d { x: 1, y: 1 };
self.draw_line((x, y), (x + w, y), col);
self.draw_line((x + w, y), (x + w, y + h), col);
self.draw_line((x + w, y + h), (x, y + h), col);
self.draw_line((x, y + h), (x, y), col);
}
/// Fill a rectangle with the top left corner at `(x, y)`
/// and the bottom right corner at `(x + w, y + h)` (both inclusive)
fn fill_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color) {
let Vi2d { x, y } = pos.into();
let Vi2d { x: w, y: h } = size.into();
for nx in x..(x + w) {
self.draw_line((nx, y), (nx, y + h), col);
}
}
/// Draw a circle with center `(x, y)` and raduis `r`
fn draw_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color) {
let Vi2d { x, y } = pos.into();
let r_i32: i32 = r.try_into().unwrap();
let x = x as i32;
let y = y as i32;
let mut x0: i32 = 0;
let mut y0: i32 = r_i32;
let mut d: i32 = 3i32 - 2i32 * r_i32;
if r == 0 {
return;
}
while y0 >= x0 {
self.draw(((x + x0), (y - y0)), col);
self.draw(((x + y0), (y - x0)), col);
self.draw(((x + y0), (y + x0)), col);
self.draw(((x + x0), (y + y0)), col);
self.draw(((x - x0), (y + y0)), col);
self.draw(((x - y0), (y + x0)), col);
self.draw(((x - y0), (y - x0)), col);
self.draw(((x - x0), (y - y0)), col);
x0 += 1;
if d < 0 {
d += 4 * x0 + 6;
} else {
y0 -= 1;
d += 4 * (x0 - y0) + 10;
}
}
}
/// Fill a circle with center `(x, y)` and raduis `r`
fn fill_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color) {
let Vi2d { x, y } = pos.into();
let r_i32: i32 = r.try_into().unwrap();
let x = x as i32;
let y = y as i32;
let mut x0: i32 = 0;
let mut y0: i32 = r_i32;
let mut d: i32 = 3 - 2 * r_i32;
if r == 0 {
return;
}
while y0 >= x0 {
self.draw_line((x - x0, y - y0), (x + x0, y - y0), col);
self.draw_line((x - y0, y - x0), (x + y0, y - x0), col);
self.draw_line((x - x0, y + y0), (x + x0, y + y0), col);
self.draw_line((x - y0, y + x0), (x + y0, y + x0), col);
x0 += 1;
if d < 0 {
d += 4 * x0 + 6;
} else {
y0 -= 1;
d += 4 * (x0 - y0) + 10;
}
}
}
/// Draw the edges of a triangle between the three points
fn draw_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color) {
let pts1: Vi2d = pts1.into();
let pts2: Vi2d = pts2.into();
let pts3: Vi2d = pts3.into();
self.draw_line(pts1, pts2, col);
self.draw_line(pts1, pts3, col);
self.draw_line(pts2, pts3, col);
}
sourcefn draw_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color)
fn draw_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color)
Draw a rectangle with the top left corner at (x, y)
and the bottom right corner at (x + w, y + h)
(both inclusive)
sourcefn fill_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color)
fn fill_rect<P: Into<Vi2d>>(&mut self, pos: P, size: P, col: Color)
Fill a rectangle with the top left corner at (x, y)
and the bottom right corner at (x + w, y + h)
(both inclusive)
sourcefn draw_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color)
fn draw_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color)
Draw a circle with center (x, y)
and raduis r
sourcefn fill_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color)
fn fill_circle<P: Into<Vi2d>>(&mut self, pos: P, r: u32, col: Color)
Fill a circle with center (x, y)
and raduis r
sourcefn draw_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color)
fn draw_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color)
Draw the edges of a triangle between the three points
Examples found in repository?
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
fn fill_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color) {
#![allow(clippy::cast_precision_loss)]
use std::cmp::{max, min};
let pts1: Vi2d = pts1.into();
let pts2: Vi2d = pts2.into();
let pts3: Vi2d = pts3.into();
self.draw_triangle(pts1, pts2, pts3, col);
let pts1 = (pts1.x as i32, pts1.y as i32);
let pts2 = (pts2.x as i32, pts2.y as i32);
let pts3 = (pts3.x as i32, pts3.y as i32);
let centroid = (
((pts1.0 + pts2.0 + pts3.0) as f32 / 3f32),
((pts1.1 + pts2.1 + pts3.1) as f32 / 3f32),
);
let lines = (
(
pts1.1 - pts2.1,
pts2.0 - pts1.0,
(pts1.1 - pts2.1) * pts2.0 + (pts2.0 - pts1.0) * pts2.1,
),
(
pts3.1 - pts1.1,
pts1.0 - pts3.0,
(pts3.1 - pts1.1) * pts1.0 + (pts1.0 - pts3.0) * pts1.1,
),
(
pts2.1 - pts3.1,
pts3.0 - pts2.0,
(pts2.1 - pts3.1) * pts3.0 + (pts3.0 - pts2.0) * pts3.1,
),
);
//dbg!(&lines);
let iterx = {
let x1 = min(min(pts1.0, pts2.0), pts3.0);
let x2 = max(max(pts1.0, pts2.0), pts3.0);
if x1 > x2 {
x2..=x1
} else {
x1..=x2
}
};
let itery = {
let y1 = min(min(pts1.1, pts2.1), pts3.1);
let y2 = max(max(pts1.1, pts2.1), pts3.1);
if y1 > y2 {
y2..=y1
} else {
y1..=y2
}
};
let l_mul = (
if (lines.0).0 as f32 * centroid.0 + (lines.0).1 as f32 * centroid.1
- (lines.0).2 as f32
>= 0f32
{
1
} else {
-1
},
if (lines.1).0 as f32 * centroid.0 + (lines.1).1 as f32 * centroid.1
- (lines.1).2 as f32
>= 0f32
{
1
} else {
-1
},
if (lines.2).0 as f32 * centroid.0 + (lines.2).1 as f32 * centroid.1
- (lines.2).2 as f32
>= 0f32
{
1
} else {
-1
},
);
for x in iterx {
for y in itery.clone() {
if ((lines.0).0 * x + (lines.0).1 * y - (lines.0).2) * l_mul.0 >= 0
&& ((lines.1).0 * x + (lines.1).1 * y - (lines.1).2) * l_mul.1 >= 0
&& ((lines.2).0 * x + (lines.2).1 * y - (lines.2).2) * l_mul.2 >= 0
{
self.draw((x, y), col);
}
}
}
}
sourcefn fill_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color)
fn fill_triangle<P: Into<Vi2d>>(&mut self, pts1: P, pts2: P, pts3: P, col: Color)
Fill the given triangle