vkit.element.line

 1# Copyright 2022 vkit-x Administrator. All Rights Reserved.
 2#
 3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses.
 4#
 5# The commercial license gives you the full rights to create and distribute software
 6# on your own terms without any SSPL license obligations. For more information,
 7# please see the "LICENSE_COMMERCIAL.txt" file.
 8#
 9# This project is also available under Server Side Public License (SSPL).
10# The SSPL licensing is ideal for use cases such as open source projects with
11# SSPL distribution, student/academic purposes, hobby projects, internal research
12# projects without external distribution, or other projects where all SSPL
13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file.
14from typing import Tuple, Sequence, Union
15
16import attrs
17
18_T = Union[float, str]
19
20
21@attrs.define
22class Line:
23    point_begin: 'Point'
24    point_end: 'Point'
25
26    ##############
27    # Conversion #
28    ##############
29    @classmethod
30    def from_xy_pairs(cls, xy_pairs: Sequence[Tuple[_T, _T]]):
31        assert len(xy_pairs) == 2
32        return cls(
33            point_begin=Point.from_xy_pair(xy_pairs[0]),
34            point_end=Point.from_xy_pair(xy_pairs[1]),
35        )
36
37    def to_xy_pairs(self):
38        return [self.point_begin.to_xy_pair(), self.point_end.to_xy_pair()]
39
40    @classmethod
41    def from_flatten_xy_pairs(cls, flatten_xy_pairs: Sequence[_T]):
42        assert len(flatten_xy_pairs) == 4
43        x0, y0, x1, y1 = flatten_xy_pairs
44        return cls(
45            point_begin=Point.create(y=y0, x=x0),
46            point_end=Point.create(y=y1, x=x1),
47        )
48
49    def to_flatten_xy_pairs(self):
50        return [
51            self.point_begin.x,
52            self.point_begin.y,
53            self.point_end.x,
54            self.point_end.y,
55        ]
56
57    ############
58    # Operator #
59    ############
60    def get_center_point(self):
61        return Point.create(
62            y=(self.point_begin.y + self.point_end.y) / 2,
63            x=(self.point_begin.x + self.point_end.x) / 2,
64        )
65
66
67# Cyclic dependency, by design.
68from .point import Point  # noqa: E402
class Line:
23class Line:
24    point_begin: 'Point'
25    point_end: 'Point'
26
27    ##############
28    # Conversion #
29    ##############
30    @classmethod
31    def from_xy_pairs(cls, xy_pairs: Sequence[Tuple[_T, _T]]):
32        assert len(xy_pairs) == 2
33        return cls(
34            point_begin=Point.from_xy_pair(xy_pairs[0]),
35            point_end=Point.from_xy_pair(xy_pairs[1]),
36        )
37
38    def to_xy_pairs(self):
39        return [self.point_begin.to_xy_pair(), self.point_end.to_xy_pair()]
40
41    @classmethod
42    def from_flatten_xy_pairs(cls, flatten_xy_pairs: Sequence[_T]):
43        assert len(flatten_xy_pairs) == 4
44        x0, y0, x1, y1 = flatten_xy_pairs
45        return cls(
46            point_begin=Point.create(y=y0, x=x0),
47            point_end=Point.create(y=y1, x=x1),
48        )
49
50    def to_flatten_xy_pairs(self):
51        return [
52            self.point_begin.x,
53            self.point_begin.y,
54            self.point_end.x,
55            self.point_end.y,
56        ]
57
58    ############
59    # Operator #
60    ############
61    def get_center_point(self):
62        return Point.create(
63            y=(self.point_begin.y + self.point_end.y) / 2,
64            x=(self.point_begin.x + self.point_end.x) / 2,
65        )
Line( point_begin: vkit.element.point.Point, point_end: vkit.element.point.Point)
2def __init__(self, point_begin, point_end):
3    self.point_begin = point_begin
4    self.point_end = point_end

Method generated by attrs for class Line.

@classmethod
def from_xy_pairs(cls, xy_pairs: Sequence[Tuple[Union[float, str], Union[float, str]]]):
30    @classmethod
31    def from_xy_pairs(cls, xy_pairs: Sequence[Tuple[_T, _T]]):
32        assert len(xy_pairs) == 2
33        return cls(
34            point_begin=Point.from_xy_pair(xy_pairs[0]),
35            point_end=Point.from_xy_pair(xy_pairs[1]),
36        )
def to_xy_pairs(self):
38    def to_xy_pairs(self):
39        return [self.point_begin.to_xy_pair(), self.point_end.to_xy_pair()]
@classmethod
def from_flatten_xy_pairs(cls, flatten_xy_pairs: Sequence[Union[float, str]]):
41    @classmethod
42    def from_flatten_xy_pairs(cls, flatten_xy_pairs: Sequence[_T]):
43        assert len(flatten_xy_pairs) == 4
44        x0, y0, x1, y1 = flatten_xy_pairs
45        return cls(
46            point_begin=Point.create(y=y0, x=x0),
47            point_end=Point.create(y=y1, x=x1),
48        )
def to_flatten_xy_pairs(self):
50    def to_flatten_xy_pairs(self):
51        return [
52            self.point_begin.x,
53            self.point_begin.y,
54            self.point_end.x,
55            self.point_end.y,
56        ]
def get_center_point(self):
61    def get_center_point(self):
62        return Point.create(
63            y=(self.point_begin.y + self.point_end.y) / 2,
64            x=(self.point_begin.x + self.point_end.x) / 2,
65        )