Skip to content

Self intersection in dashed plotter #182

Description

@AdyanOchirov
const std = @import("std");
const z2d = @import("z2d");

const Point = struct { x: f64, y: f64 };

pub fn main(init: std.process.Init) !void {
    const points: []const Point = &.{
        .{ .x = 50 + 0, .y = 150 + 0 },
        .{ .x = 50 + 100, .y = 150 + 100 },
        .{ .x = 50 + 200, .y = 150 + -100 },
        .{ .x = 50 + 1, .y = 150 + -50 },
    };
    var surface: z2d.Surface = try .init(.image_surface_rgba, init.gpa, 300, 300);
    defer surface.deinit(init.gpa);
    surface.paintPixel(.{ .rgba = .{ .r = 0, .g = 0, .b = 0, .a = 255 } });
    var context: z2d.Context = .init(init.io, init.gpa, &surface);
    defer context.deinit();

    try context.moveTo(points[0].x, points[0].y);
    for (1..points.len) |i| {
        try context.lineTo(points[i].x, points[i].y);
    }
    try context.closePath();

    var transform = z2d.Transformation.identity;
    transform = transform.scale(3.0, 3.0);
    context.setTransformation(transform);

    context.setDashes(&.{ 10, 20, 30 });
    context.setSourceToPixel(.{ .rgba = .{ .r = 255, .g = 255, .b = 255, .a = 255 } });
    try context.stroke();

    try z2d.png_exporter.writeToPNGFile(init.io, surface, "test.png", .{});
}

Produces the following on 47fc1030c1636dcfb75ea1cc238f706dd1feb379
Image

Here is geometry of broken dash

Image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions