ToyDesigner/doc/shape.md

3.2 KiB
Raw Permalink Blame History

ToyDesigner形状Shape

源码:shape.py

Point

点是 ToyDesigner 中最基本的形状,它代表着坐标系中的一个点。声明方式为point = Point(x, y),参数xy分别代表着该点的横坐标和纵坐标。

注意ToyDesigner 中的坐标系为直角坐标系。

# 示例
from toydesigner.shape import Point

# 声明一个点
point = Point(1, 2)

# 打印点的坐标
print(point.x, point.y)  # 1 2

# 弹出GUI展示点的位置
point.show()

直线Line

直线是由两个点组成的线段。 声明方式为line = Line(point1, point2),参数point1point2分别是该线段的两个端点。也可使用line = Line((x1, y1), (x2, y2)),参数x1y1x2y2分别是该直线的两个端点的横、纵坐标。

注意直线的两个端点必须是不同的点。否则将会抛出ValuError异常。

# 示例
from toydesigner.shape import Point, Line

# 声明两个点
point1 = Point(1, 2)
point2 = Point(3, 4)

# 声明一个直线
line = Line(point1, point2)

# 另一种写法
line = Line((1, 2), (3, 4))

矩形Rectangle

矩形是由两个点组成的矩形,它可以用来绘制矩形、圆角矩形等图形。 声明方式为rectangle = Rectangle(point1, point2),参数point1point2分别是该矩形左上角和右下角两个端点,也可使用rectangle = Rectangle((x1, y1), (x2, y2)),参数x1y1x2y2分别是该矩形左上角和右下角两个端点的横、纵坐标。

注意矩形的两个端点必须是不同的点。否则将会抛出ValuError异常。

# 示例
from toydesigner.shape import Point, Rectangle


# 声明两个点
point1 = Point(1, 2)
point2 = Point(3, 4)

# 声明一个矩形
rectangle = Rectangle(point1, point2)

# 另一种写法
rectangle = Rectangle((1, 2), (3, 4))

Circle

圆是由中心点和半径组成的图形。 声明方式为circle = Circle(center, radius),参数center是该圆的中心点,参数radius是该圆的半径,也可使用circle = Circle((x, y), r),参数xy是该圆的中心点的横、纵坐标,参数r是该圆的半径。

注意radius必须大于0。否则将会抛出ValueError异常。

# 示例
from toydesigner.shape import Point, Circle

# 声明一个中心点和半径
center = Point(1, 2)
radius = 3

# 声明一个圆
circle = Circle(center, radius)

# 另一种写法
circle = Circle((1, 2), 3)

多边形Polygon

多边形是由多个点组成的图形。 声明方式为polygon = Polygon(points),参数points是该多边形的点列表,列表中的每个元素都是一个点。也可使用tuple(x, y)代替Point(x, y),声明方式为polygon = Polygon([(x1, y1), (x2, y2), (x3, y3), ...]),参数x1y1x2y2x3y3分别是该多边形的点的横、纵坐标。

# 示例
from toydesigner.shape import Point, Polygon

# 声明点列表
points = [Point(1, 2), Point(3, 4), Point(5, 6)]

# 声明一个多边形
polygon = Polygon(points)

# 另一种写法
polygon = Polygon([(1, 2), (3, 4), (5, 6)])