|
本帖最后由 mao_jin_dao 于 2020-12-23 22:58 编辑
代码1 圆
- from machine import Pin, I2C
- from ssd1306 import SSD1306_I2C
- import math
- def light_dot(x,y):
- oled.pixel(x,y,1)
- def draw_circle(x,y,r,fill=0):
- '''
- 绘制圆形
- :param x,y 圆心坐标
- :param r 圆心半径
- :param fill 0 不填充 1 填充
- '''
- angleList = [0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.7853981633974483, 0.8, 0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.5707963267948966, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2.0, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.356194490192345, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3.0, 3.05, 3.1, 3.141592653589793, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.9269908169872414, 3.95, 4.0, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 4.6, 4.65, 4.7, 4.71238898038469, 4.75, 4.8, 4.85, 4.9, 4.95, 5.0, 5.05, 5.1, 5.15, 5.2, 5.25, 5.3, 5.35, 5.4, 5.45, 5.497787143782138, 5.5, 5.55, 5.6, 5.65, 5.7, 5.75, 5.8, 5.85, 5.9, 5.95, 6.0, 6.05, 6.1, 6.15, 6.2, 6.25, 6.283185307179586];
- for i in angleList:
- light_dot(x+round(math.sin(i)*r),y+round(math.cos(i)*r))
- if fill:
- powR = math.pow(r,2)
- for xx in range(x-r,x+r):
- for yy in range(y-r,y+r):
- if ( (math.pow(xx-x,2)+math.pow(yy-y,2)) < powR):
- light_dot(xx,yy)
- oled.show()
-
- if __name__ == '__main__':
-
- i2c = I2C(scl=Pin(5), sda=Pin(4))
- oled = SSD1306_I2C(128, 64, i2c)
- oled.fill(0)
- oled.show()
-
- draw_circle(25,25,20,1)
- draw_circle(75,25,20,1)
- draw_circle(50,32,30)
复制代码 代码2 圆弧与圆
- from machine import Pin, I2C
- from ssd1306 import SSD1306_I2C
- import math
- def light_dot(x, y):
- oled.pixel(x, y, 1)
- def draw_circular_arc(x, y, r, start_angle, end_angle, isCircular=0):
- '''
- 绘制圆弧
- :x,y 圆心坐标
- :r 半径
- :start_angle,end_angle 角度范围
- :isCircular 是否为圆 未使用abs(start_angle-end_angle)>360,因为与使用者自由。
- '''
- # 0度的位置有点怪,顺时针转了90度。+90度作为补偿。
- angleList = [math.radians(i+90) for i in range(start_angle, end_angle+1)]
- for i in angleList:
- light_dot(x+round(math.sin(i)*r), y+round(math.cos(i)*r))
- if not isCircular:
- oled.show()
- def draw_circular(x, y, r, fill=0):
- '''
- 绘制圆,并决定是否填充
- :x,y 圆心坐标
- :r 半径
- :fill 是否填充 0 否 1 是
- '''
- draw_circular_arc(x, y, r, 0, 360, isCircular=1)
- if fill:
- powR = math.pow(r, 2)
- for xx in range(x-r, x+r):
- for yy in range(y-r, y+r):
- if ((math.pow(xx-x, 2)+math.pow(yy-y, 2)) < powR):
- light_dot(xx, yy)
- oled.show()
- if __name__ == '__main__':
- i2c = I2C(scl=Pin(5), sda=Pin(4))
- oled = SSD1306_I2C(128, 64, i2c)
- oled.fill(0)
- draw_circular_arc(25, 25, 12, 20, 160)
- draw_circular_arc(50, 25, 12, 20, 160)
- draw_circular_arc(38, 35, 15, 200, 340)
- draw_circular(90, 25, 20, 1)
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|