点的构成
def setup():
size(600, 600)
noLoop()
noStroke()
def draw():
background(0)
for i in range(10):
with pushMatrix():
translate(300, 300)
for j in range(i * (15-i)):
with pushMatrix():
rotate(radians(j * 360/(i*(15-i))))
ellipse(i * 20,0,i * 2, i * 2)
![](https://img.haomeiwen.com/i1108512/dd903d4a10369af4.png)
点的面积渐变,同时圆形规律排列
![](https://img.haomeiwen.com/i1108512/6bf2cd3c7112f021.png)
对于小学生,图形大小、半径、每一圈圆的数量可以用简单的线性关系表示,对于中学生,可以用复杂的三角函数完成计算。
*如何保证每一圈上的圆都是相切的?如何画任意大小随机的圆,圆之间都是相切的。
彩色同心圆
def setup():
size(600, 600)
noLoop()
noStroke()
colorMode(HSB)
def draw():
background(0)
for i in range(10):
with pushMatrix():
translate(300, 300)
for j in range(i * (15-i)):
with pushMatrix():
rotate(radians(j * 360/(i*(15-i))))
fill(color(random(360),
random(360),
random(100, 360),
))
ellipse(i * 20,0,i * 2, i * 2)
# arc(i * 20,0,i * 2, i * 2, radians(-90), radians(90))
利用HSB颜色模式,可以保证颜色的鲜艳:
![](https://img.haomeiwen.com/i1108512/63c52d404f617c35.png)
点的排列中有规律,比如大小,位置,也有随机,比如颜色。
hsb颜色好,随机出来的颜色很鲜艳。
线的错觉
两条平行直线在交叉斜线的背景下会产生错觉
def setup():
size(600, 300)
noLoop()
noStroke()
colorMode(HSB)
stroke(0)
def draw():
background(255)
gap = 20
for i in range(width/gap+1):
print(i)
line(width/2,height/4,i*gap,height/2)
line(width/2,height*3/4,i*gap,height/2)
line(0, height/3+30,width,height/3+30)
line(0, height*2/3-30,width,height*2/3-30)
![](https://img.haomeiwen.com/i1108512/c5a764aa188efd23.png)
def setup():
size(600, 300)
noLoop()
noStroke()
colorMode(HSB)
stroke(0)
def draw():
background(255)
gap = 20
for i in range(width/gap+1):
print(i)
line(i*gap,height/4,width/2,height/2)
line(i*gap,height*3/4, width/2,height/2)
line(0, height/3,width,height/3)
line(0, height*2/3,width,height*2/3)
![](https://img.haomeiwen.com/i1108512/bfb64ae65bc383c9.png)
平行线弯曲的例子,涉及到了循环,line的使用,不太复杂,但是非常有趣,学生可以自己编程验证,学生学习到第2课就可以完成这样的作业。
哪个大
from random import randint
def setup():
global angle
size(600, 600)
angle = 0
noLoop()
def draw():
global angle
background(255)
fill(0)
with pushMatrix():
translate(width/4, height/2)
ellipse(0, 0, 100, 100)
for i in range(5):
with pushMatrix():
rotate(radians(72*i))
w = randint(20, 30)
ellipse(80, 0, w, w)
with pushMatrix():
translate(width*3/4, height/2)
ellipse(0, 0, 100, 100)
for i in range(5):
with pushMatrix():
rotate(radians(72*i))
w = randint(40, 55)
ellipse(80, 0, w, w)
![](https://img.haomeiwen.com/i1108512/dcb6044f52e9b375.png)
还有就是反套路,本来按照错觉,黑色的物体是容易收缩的,但是,我们可以故意把黑色物体的尺寸调大,形成视觉上的平衡效果。
def setup():
global w, h
size(400, 200)
background(255)
noStroke()
rectMode(CENTER)
w = width/2
h = height
def draw():
global w, h
background(255)
fill(255)
rect(width/4, height/2, w, h)
fill(0)
ellipse(width/4, height/2, w/2+4, h/2+4)
fill(0)
rect(width*3/4, height/2, w, h)
fill(255)
ellipse(width*3/4, height/2, w/2, h/2)
![](https://img.haomeiwen.com/i1108512/85ec0bc88dcb480a.png)
这里看上去是一样大的,其实是黑色的大,黑色有收缩的趋势,看上去仍然一样大是因为我们放大了参数。
![](https://img.haomeiwen.com/i1108512/05857d4375076bf0.png)
趣味视错觉与编程的验证,我们在读书的是,看到很多视错觉,然而我们可以自己构建这样的图
有多少黑点
def setup():
size(600, 600)
noLoop()
noStroke()
# colorMode(HSB)
# stroke(0)
fill(0)
def draw():
background(100)
gap = 10
w = 50
w1 = gap + w
for i in range(width//(gap + w)):
for j in range(height//(gap+w)):
fill(0)
rect(w1 * i, w1 * j, w, w)
fill(255)
ellipse(w1 * i-gap/2, w1 * j-gap/2, gap, gap)
![](https://img.haomeiwen.com/i1108512/cd95cba082bc1264.png)
这里用到的函数与前面4课中的类似,是非常好的迁移作业。
扭曲的圆和正方形
def setup():
size(600, 600)
noLoop()
noStroke()
# colorMode(HSB)
stroke(0)
rectMode(CENTER)
noFill()
def draw():
background(255)
with pushMatrix():
translate(width/2, height/2)
for i in range(36):
with pushMatrix():
rotate(radians(10*i))
line(0,0,width/2,0)
# ellipse(0, 200, 10, 10)
rect(-100, 0, 100, 100)
ellipse(100, 0, 100, 100)
![](https://img.haomeiwen.com/i1108512/0076fc187610827d.png)
网友评论