-
Notifications
You must be signed in to change notification settings - Fork 65
/
ICanvas.cs
37 lines (36 loc) · 1.26 KB
/
ICanvas.cs
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
using System;
using System.Drawing;
namespace CSharpMath.Rendering.FrontEnd {
public interface ICanvas {
float Width { get; }
float Height { get; }
Color DefaultColor { get; set; }
Color? CurrentColor { get; set; }
PaintStyle CurrentStyle { get; set; }
Path StartNewPath();
void DrawLine(float x1, float y1, float x2, float y2, float lineThickness);
void StrokeRect(float left, float top, float width, float height);
void FillRect(float left, float top, float width, float height);
void Save();
void Translate(float dx, float dy);
void Scale(float sx, float sy);
void Restore();
}
public static class CanvasExtensions {
public static void StrokeLineOutline
(this ICanvas c, float x1, float y1, float x2, float y2, float lineThickness) {
var dx = Math.Abs(x2 - x1);
var dy = Math.Abs(y2 - y1);
var length = (float)Math.Sqrt((double)dx * dx + (double)dy * dy);
var halfThickness = lineThickness / 2;
var px = dx / length * halfThickness;
var py = dy / length * halfThickness;
using var p = c.StartNewPath();
p.MoveTo(x1 - py, y1 + px);
p.LineTo(x1 + py, y1 - px);
p.LineTo(x2 + py, y2 - px);
p.LineTo(x2 - py, y2 + px);
p.CloseContour();
}
}
}