render vertices in addition to edges

This commit is contained in:
2026-02-17 00:54:43 +01:00
parent d6ce1a94f5
commit fba493ea93
6 changed files with 47 additions and 44 deletions

View File

@ -21,31 +21,47 @@ auto Renderer::Map(const Vector2 &a) -> Vector2 {
return Vector2((1.0 + a.x) / 2.0 * width, (1.0 - a.y) * height / 2.0);
}
auto Renderer::Transform(Edge2Set &edges, const MassSpringSystem &mass_springs,
auto Renderer::Transform(Edge2Set &edges, Vertex2Set &vertices,
const MassSpringSystem &mass_springs,
const float angle, const float distance) -> void {
edges.clear();
const float cos_angle = cos(angle);
const float sin_angle = sin(angle);
edges.clear();
for (const auto &spring : mass_springs.springs) {
Vector2 at = Map(Project(Translate(
Vector2 a = Map(Project(Translate(
Rotate(spring.massA.position, cos_angle, sin_angle), distance)));
Vector2 bt = Map(Project(Translate(
Vector2 b = Map(Project(Translate(
Rotate(spring.massB.position, cos_angle, sin_angle), distance)));
edges.emplace_back(at, bt);
edges.emplace_back(a, b);
}
// This is duplicated work, but easy to read
vertices.clear();
for (const auto &mass : mass_springs.masses) {
Vector3 a =
Translate(Rotate(mass.position, cos_angle, sin_angle), distance);
Vector2 b = Map(Project(a));
vertices.emplace_back(b.x, b.y, a.z);
}
}
auto Renderer::Draw(const Edge2Set &edges) -> void {
auto Renderer::DrawMassSprings(const Edge2Set &edges,
const Vertex2Set &vertices) -> void {
BeginTextureMode(render_target);
ClearBackground(RAYWHITE);
for (const auto &[a, b] : edges) {
DrawLine(a.x, a.y, b.x, b.y, EDGE_COLOR);
DrawCircle(a.x, a.y, VERTEX_SIZE, VERTEX_COLOR);
DrawCircle(b.x, b.y, VERTEX_SIZE, VERTEX_COLOR);
}
for (const auto &a : vertices) {
// Increase the perspective perception by squaring the z-coordinate
const float size = VERTEX_SIZE / (a.z * a.z);
DrawRectangle(a.x - size / 2.0, a.y - size / 2.0, size, size, VERTEX_COLOR);
}
DrawLine(0, 0, 0, height, BLACK);
EndTextureMode();
BeginDrawing();