数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 1797|回复: 1

[Arduino] [esp8266_micropython] ssd1306绘制矩形V2

[复制链接]
发表于 2021-1-6 23:43:15 | 显示全部楼层 |阅读模式
代码
1. 较前一版代码改进了线段斜率绝对值大于1时的显示模式
2. 为矩形添加process模式,但未添加控制process百分值得参数

  1. from machine import Pin, I2C
  2. from ssd1306 import SSD1306_I2C
  3. import math
  4. import time


  5. def light_dot(x, y):
  6.     oled.pixel(x, y, 1)


  7. def draw_line(x1, y1, x2, y2, isShow=1):
  8.     '''
  9.     绘制线段
  10.     abs(k) > 1 时,调换x,y进行处理
  11.     :param: x1 y1 x2 y2 线段两端
  12.     :param: isShow 是否显示
  13.     :return: None
  14.     '''
  15.     if (x1 == x2):
  16.         step = 1
  17.         if (y1 > y2):
  18.             step *= -1
  19.         while (y1 != y2):
  20.             light_dot(x1, y1)
  21.             y1 = y1 + step
  22.         light_dot(x2, y2)
  23.     else:
  24.         # y = kx+a
  25.         k = (y2 - y1) / (x2 - x1)
  26.         if (abs(k) <= 1):
  27.           a = y1 - (k * x1)
  28.           step = 1
  29.           if (x1 > x2):
  30.               step *= -1
  31.           while (x1 != x2):
  32.               light_dot(x1, round(k * x1 + a))
  33.               x1 += step
  34.           light_dot(x2, y2)
  35.         else:
  36.           k = (x2-x1)/(y2-y1)
  37.           a = x1-(k*y1)
  38.           step = 1
  39.           if (y1 > y2):
  40.             step *= -1
  41.           while (y1 != y2):
  42.             light_dot(round(k*y1+a),y1)
  43.             y1 += step
  44.           light_dot(x2, y2)
  45.          
  46.     if isShow:
  47.         oled.show()


  48. def draw_rectangle(x, y, length, width, isFill=0, isProcess=0):
  49.     '''
  50.     绘制矩形
  51.     :param: x y 矩形左上角
  52.     :param: length width 长和宽
  53.     :isFill: 是否填充 0 否 1 是
  54.     :isProcess: 是否显示为进度条 无用
  55.     :return: None
  56.     '''
  57.     point_upper_left = (x, y)
  58.     point_upper_right = (x + length, y)
  59.     point_bottom_left = (x, y + width)
  60.     point_bottom_right = (x + length, y + width)

  61.     draw_line(point_upper_left[0], point_upper_left[1],
  62.               point_upper_right[0], point_upper_right[1])
  63.     draw_line(point_upper_left[0], point_upper_left[1],
  64.               point_bottom_left[0], point_bottom_left[1])
  65.     draw_line(point_bottom_left[0], point_bottom_left[1],
  66.               point_bottom_right[0], point_bottom_right[1])
  67.     draw_line(point_upper_right[0], point_upper_right[1],
  68.               point_bottom_right[0], point_bottom_right[1])


  69.     if isFill:
  70.         for xx in range(x, x + length + 1):
  71.             draw_line(xx, y, xx, y + width, 0)
  72.             if isProcess:
  73.               oled.show()
  74.     if not isProcess:
  75.       oled.show()


  76. def draw_diamond(x, y, side_length, angle):
  77.     '''
  78.     绘制菱形
  79.     :param: x y 菱形顶点
  80.     :param: side_length 边长
  81.     :angle: 顶角 [0,90]
  82.     :return: None
  83.     '''
  84.     xx = abs(round(math.sin(angle / 2) * side_length))
  85.     yy = abs(round(math.cos(angle / 2) * side_length))

  86.     point_upper = (x, y)
  87.     point_left = (x - xx, y + yy)
  88.     point_right = (x + xx, y + yy)
  89.     point_bottom = (x, y + yy * 2)

  90.     draw_line(point_upper[0], point_upper[1], point_left[0], point_left[1], isShow=0)
  91.     draw_line(point_upper[0], point_upper[1], point_right[0], point_right[1], isShow=0)
  92.     draw_line(point_bottom[0], point_bottom[1], point_left[0], point_left[1], isShow=0)
  93.     draw_line(point_bottom[0], point_bottom[1], point_right[0], point_right[1], isShow=0)


  94. if __name__ == '__main__':
  95.     i2c = I2C(scl=Pin(5), sda=Pin(4))
  96.     oled = SSD1306_I2C(128, 64, i2c)
  97.     oled.fill(0)

  98.     draw_rectangle(10, 10, 30, 20)
  99.     draw_diamond(90, 10, 30, 45)
  100.     draw_rectangle(10, 40, 60, 20, 1, 1)
  101.   
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
发表于 2021-1-7 10:47:14 | 显示全部楼层
谢谢楼主分享  学习一下!绘制矩形的方法
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-6-29 14:52 , Processed in 0.187200 second(s), 12 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

快速回复 返回顶部 返回列表