diff --git a/doc/shape.md b/doc/shape.md new file mode 100644 index 0000000..1f8855d --- /dev/null +++ b/doc/shape.md @@ -0,0 +1,110 @@ +# ToyDesigner形状(Shape) +> 源码:[shape.py](https://gitea.winkinshly.site/songsenand/ToyDesigner/blob/main/toydesigner/shape.py) + +## 点(Point) + +点是 ToyDesigner 中最基本的形状,它代表着坐标系中的一个点。声明方式为`point = Point(x, y)`,参数`x`和`y`分别代表着该点的横坐标和纵坐标。 + +> 注意:ToyDesigner 中的坐标系为直角坐标系。 + +```python +# 示例 +from toydesigner.shape import Point + +# 声明一个点 +point = Point(1, 2) + +# 打印点的坐标 +print(point.x, point.y) # 1 2 + +# 弹出GUI展示点的位置 +point.show() +``` + +## 直线(Line) + +直线是由两个点组成的线段。 +声明方式为`line = Line(point1, point2)`,参数`point1`和`point2`分别是该线段的两个端点。也可使用`line = Line((x1, y1), (x2, y2))`,参数`x1`、`y1`、`x2`、`y2`分别是该直线的两个端点的横、纵坐标。 + +> 注意:直线的两个端点必须是不同的点。否则将会抛出ValuError异常。 + +```python +# 示例 +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)`,参数`point1`和`point2`分别是该矩形左上角和右下角两个端点,也可使用`rectangle = Rectangle((x1, y1), (x2, y2))`,参数`x1`、`y1`、`x2`、`y2`分别是该矩形左上角和右下角两个端点的横、纵坐标。 + +> 注意:矩形的两个端点必须是不同的点。否则将会抛出ValuError异常。 + +```python +# 示例 +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)`,参数`x`、`y`是该圆的中心点的横、纵坐标,参数`r`是该圆的半径。 + +> 注意:radius必须大于0。否则将会抛出ValueError异常。 + +```python +# 示例 +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), ...])`,参数`x1`、`y1`、`x2`、`y2`、`x3`、`y3`分别是该多边形的点的横、纵坐标。 + +```python +# 示例 +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)]) +``` +