1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use ggez::{
  graphics::{Color, DrawMode, Mesh, MeshBuilder, Rect},
  nalgebra::{Point2, Translation2, Rotation2},
  Context,
};

use std::f32::consts::FRAC_PI_3;

pub fn create_circle(x: f32, y: f32, radius: f32, color: Color, ctx: &mut Context) -> Mesh {
  MeshBuilder::new()
    .circle(DrawMode::fill(), Point2::new(x, y), radius, 0.01, color)
    .build(ctx)
    .unwrap()
}

pub fn create_rect(
  x: f32,
  y: f32,
  width: f32,
  height: f32,
  color: Color,
  ctx: &mut Context,
) -> Mesh {
  let rect = Rect::new(x, y, width, height);
  MeshBuilder::new()
    .rectangle(DrawMode::fill(), rect, color)
    .build(ctx)
    .unwrap()
}

pub fn create_square(x: f32, y: f32, side: f32, color: Color, ctx: &mut Context) -> Mesh {
  let rect = Rect::new(x, y, side, side);
  MeshBuilder::new()
    .rectangle(DrawMode::fill(), rect, color)
    .build(ctx)
    .unwrap()
}

pub fn create_triangle(points: &[Point2<f32>; 3], color: Color, ctx: &mut Context) -> Mesh {
  MeshBuilder::new()
    .polygon(DrawMode::fill(), points, color)
    .unwrap()
    .build(ctx)
    .unwrap()
}

fn get_equilateral_points(x: f32, y: f32, side: f32) -> [Point2<f32>; 3] {
  let radius: f32 = side / (3.0 as f32).sqrt();
  let point = Point2::new(0.0, 0.0 - radius);
  let translation = Translation2::new(x, y);
  [
    translation * point,
    translation * (Rotation2::new(2.0 * FRAC_PI_3) * point),
    translation * (Rotation2::new(4.0 * FRAC_PI_3) * point),
  ]
}

pub fn create_equilateral_triangle(x: f32, y: f32, side: f32, color: Color, ctx: &mut Context) -> Mesh {
  create_triangle(&get_equilateral_points(x, y, side), color, ctx)
}