DVUI

rotate

Rotate vertexes around origin by radians (positive clockwise).

Parameters

#
self:*Triangles
*Triangles
origin:Point.Physical
Point.Physical
radians:f32
f32

Source

Implementation

#
pub fn rotate(self: *Triangles, origin: Point.Physical, radians: f32) void {
    if (radians == 0) return;

    const cos = @cos(radians);
    const sin = @sin(radians);

    for (self.vertexes) |*v| {
        // get vector from origin to point
        const d = v.pos.diff(origin);

        // rotate vector
        const rotated: Point.Physical = .{
            .x = d.x * cos - d.y * sin,
            .y = d.x * sin + d.y * cos,
        };

        v.pos = origin.plus(rotated);
    }

    // recalc bounds
    var points: [4]Point.Physical = .{
        self.bounds.topLeft(),
        self.bounds.topRight(),
        self.bounds.bottomRight(),
        self.bounds.bottomLeft(),
    };

    for (&points) |*p| {
        // get vector from origin to point
        const d = p.diff(origin);

        // rotate vector
        const rotated: Point.Physical = .{
            .x = d.x * cos - d.y * sin,
            .y = d.x * sin + d.y * cos,
        };

        p.* = origin.plus(rotated);
    }

    self.bounds.x = @min(points[0].x, points[1].x, points[2].x, points[3].x);
    self.bounds.y = @min(points[0].y, points[1].y, points[2].y, points[3].y);
    self.bounds.w = @max(points[0].x, points[1].x, points[2].x, points[3].x);
    self.bounds.w -= self.bounds.x;
    self.bounds.h = @max(points[0].y, points[1].y, points[2].y, points[3].y);
    self.bounds.h -= self.bounds.y;
}