2018-12-21 13:56:48 [显示全部楼层]
2884浏览
查看: 2884|回复: 0

[入门教程] 【掌控】新技能: 画圆

[复制链接]
本帖最后由 rzyzzxw 于 2018-12-21 17:20 编辑

掌控新技能:画圆

【掌控】新技能: 画圆图1

【新技能】

oled.circle(x, y, radius, c)

绘制圆

x 、y -圆心坐标。radius -圆半径大小  c -为1时,像素点亮;c 为0时,像素点灭。


oled.fill_circle(x, y, radius, c)


x 、y -圆心坐标。radius -圆半径大小  c -为1时,像素点亮;c 为0时,像素点灭。

【空心圆】
# 画空心圆代码
from mpython import *
# 半径 中心x y坐标
radius = 10
center_x = 64
center_y = 32

oled.fill(0)
oled.circle(center_x, center_y, radius, 1)
oled.show()

【实心圆】
[mw_shl_code=python,true]# 画实心圆代码
from mpython import *
# 半径 中心x y坐标
radius = 8
center_x = 64
center_y = 32

oled.fill(0)
oled.fill_circle(center_x, center_y, radius, 1)
oled.show()[/mw_shl_code]

【右移的圆】
[mw_shl_code=python,true]# 右移的圆
from mpython import *

# 半径 中心x y坐标
radius = 8
while 1:
    for i in range(128):
        center_x = i
        center_y = 32
        oled.fill(0)
        oled.circle(center_x, center_y, radius, 1)
        oled.show()
        [/mw_shl_code]

【重力小球】
[mw_shl_code=python,true]# 重力小球
from mpython import *

# 半径 中心x y坐标
radius = 10
        
center_x = 0
center_y = 0
while True:
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    oled.fill(0)
    center_x = 128-int(x*64+64)
    center_y = int(y*32+32)
    oled.fill_circle(center_x, center_y, radius, 1)
    oled.show()[/mw_shl_code]


上面的代码中,要理解的是这几句的算法:
    x = accelerometer.get_y()
    y = accelerometer.get_x()

    center_x = 128-int(x*64+64)
    center_y = int(y*32+32)

【小知识】
为什么是X获取加速度Y向,而y获取加速度X向的值。

加速度传感器能够测量由于重力引起的加速度,传感器在加速过程中,通过对质量块所受惯性力的测量,利用牛顿第二定律获得加速度值。掌控板上的加速度计可测量加速度,测量范围为 -2g 到 +2g 之间。

掌控板的测量沿3个轴,每个轴的测量值是正数或负数,正轴越趋近重力加速度方向,其数值往正数方向增加,反之往负数方向减小,当读数为 0 时,表示沿着该特定轴“水平”放置。

  • X - 向前和向后倾斜。
  • Y - 向左和向右倾斜。
  • Z - 上下翻转。


加速度的读数范围是1和-1之间,左向为正,右向为负。
所以在把加速度值转化为OLED坐标,用了这个式子:center_x = 128-int(x*64+64)    int()是取整函数
当加速度y为1时,状态是向左倾,x*64+64=128 ,取整后128-int(x*64+64)=0,是屏幕的最左点。
当加速度y为-1时,状态是向右倾,x*64+64=0 ,取整后128-int(x*64+64)=128,是屏幕的最左点。
center_y = int(y*32+32)
向上向下倾类似。

【上升气泡】
[mw_shl_code=python,true]# 上升气泡
from mpython import *

# 半径 中心x y坐标
radius = 10
        
center_x = 0
center_y = 0
while True:
    x = accelerometer.get_y()
    y = accelerometer.get_x()
    oled.fill(0)
    center_x = 128-int(x*64+64)
    center_y = int(y*32+32)
    oled.circle(128-center_x, 64-center_y, radius, 1)
    oled.show()

[/mw_shl_code]


这个代码,重点在这里

oled.circle(128-center_x, 64-center_y, radius, 1)

通过减法实现方向改变。

【掌控】新技能: 画圆图2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail