1. 使用Python的Turtle库
```python
import turtle
def rect():
for i in range(4):
forward(100) 边长为100
right(90)
主程序
d = 100
rect()
turtle.done()
```
2. 使用JavaScript
```javascript
function drawSquare(rows) {
for (let i = 1; i <= rows; i++) {
for (let j = 1; j <= rows; j++) {
document.write("*");
}
document.write("\n");
}
}
drawSquare(10); // 输入正方形的行数
```
3. 使用VB编程
```vb
Private Sub Command1_Click()
Shape1.Shape = 1 ' 正方形
End Sub
```
4. 使用GeoCodeStudio库
```python
from geocodestudio import *
canvas = Canvas(width=800, height=800, background_color=(255, 255, 255))
定义方形参数
square_top_left_x, square_top_left_y = 100, 100 左上角坐标
square_side_length = 300 边长
绘制方形
canvas.draw_rectangle(square_top_left_x, square_top_left_y, square_side_length, square_side_length, color=(255, 0, 0))
显示画布
canvas.show()
```
5. 使用简单工厂模式
```python
class Shape:
def draw(self):
pass
class Circle(Shape):
def draw(self):
print("画一个圆")
class Square(Shape):
def draw(self):
print("画一个正方形")
class ShapeFactory:
@staticmethod
def create_shape(shape_type):
if shape_type == "circle":
return Circle()
elif shape_type == "square":
return Square()
else:
raise ValueError(f"未知的形状类型: {shape_type}")
测试简单工厂模式
factory = ShapeFactory()
square = factory.create_shape("square")
square.draw() 输出: 画一个正方形
```
这些示例展示了如何在不同的编程环境中绘制正方形。你可以根据自己的需求和使用的编程语言选择合适的方法。