Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PointExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d2dfb90fb04d9f50822a39bea6fbcf6bb1ed0e8 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Drawing;

namespace UVtools.Core.Extensions;

public static class PointExtensions
{
    public static double FindLength(Point start, Point end) => Math.Sqrt(Math.Pow(end.Y - start.Y, 2) + Math.Pow(end.X - start.X, 2));

    public static bool IsAnyNegative(this Point point) => point.X < 0 || point.Y < 0;
    public static bool IsBothNegative(this Point point) => point.X < 0 && point.Y < 0;
    public static bool IsBothZeroOrPositive(this Point point) => point.X >= 0 && point.Y >= 0;

    public static Point Rotate(this Point point, double angleDegree, Point pivot = default)
    {
        if (angleDegree % 360 == 0) return point;
        double angle = angleDegree * Math.PI / 180;
        double cos = Math.Cos(angle);
        double sin = Math.Sin(angle);
        int dx = point.X - pivot.X;
        int dy = point.Y - pivot.Y;
        double x = cos * dx - sin * dy + pivot.X;
        double y = sin * dx + cos * dy + pivot.Y;

        return new((int)Math.Round(x), (int)Math.Round(y));
    }

    public static PointF Rotate(this PointF point, double angleDegree, PointF pivot = default)
    {
        if (angleDegree % 360 == 0) return point;
        double angle = angleDegree * Math.PI / 180;
        double cos = Math.Cos(angle);
        double sin = Math.Sin(angle);
        double dx = point.X - pivot.X;
        double dy = point.Y - pivot.Y;
        double x = cos * dx - sin * dy + pivot.X;
        double y = sin * dx + cos * dy + pivot.Y;

        return new((float) x, (float) y);
    }

    public static void Rotate(Point[] points, double angleDegree, Point pivot = default)
    {
        for (int i = 0; i < points.Length; i++)
        {
            points[i] = points[i].Rotate(angleDegree, pivot);
        }
    }

    public static void Rotate(PointF[] points, double angleDegree, PointF pivot = default)
    {
        for (int i = 0; i < points.Length; i++)
        {
            points[i] = points[i].Rotate(angleDegree, pivot);
        }
    }

    public static Point Invert(this Point point) => new(-point.X, -point.Y);

    public static PointF Invert(this PointF point) => new(-point.X, -point.Y);

    public static Point OffsetBy(this Point point, int value)=> new(point.X + value, point.Y + value);
    public static Point OffsetBy(this Point point, int x, int y) => new(point.X + x, point.Y + y);
    public static Point OffsetBy(this Point point, Point other) => new(point.X + other.X, point.Y + other.Y);


    public static Point Half(this Point point) => new(point.X / 2, point.Y / 2);
    public static PointF Half(this PointF point) => new(point.X / 2, point.Y / 2);
    public static Point ToPoint(this PointF point) => new((int) Math.Round(point.X), (int) Math.Round(point.Y));



    public static Size ToSize(this Point point) => new(point.X, point.Y);

    public static SizeF ToSize(this PointF point) => new(point.X, point.Y);

        

}