当前位置:网站首页>[p5.js] actual copy - chess board
[p5.js] actual copy - chess board
2022-07-28 17:49:00 【Zhigan doctrine circle Maojun】

Analyze the law of motion
It can be found by careful observation that , The black squares and white squares are scaled in turn , While the black square returns to its maximum size , The white square began to shrink ,vice versa
Analyze the turning point
1. The expansion and contraction of black squares <-> The expansion and contraction of white squares
2. Zoom in <-> narrow
Code implementation
After finding the turning point , The idea of the code is very clear . First, distinguish two boards —— Blackboard and whiteboard , Place white squares and black squares respectively , That is, define a converted signal variable .
stay draw Method to define a self increasing variable , This variable is related to the transformation of the size of the square . When a color grid shrinks and enlarges to the maximum , Transform a Boolean value , Start scaling the squares of two colors .
let startScale; // Initial size of the square
let endScale; // The limit of square reduction
let returned; // Whether to reply to the initial size
let turnwhite; // Whether to change from black square to white square
function setup() {
createCanvas(320, 320);
startScale = 40;
endScale = 20;
returned = false;
turnwhite = false;
}
function draw() {
if(turnwhite == false) { // White board, black square
if(returned == false){ // narrow
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // Zoom in
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
whiteboard(startScale);
}
if(turnwhite == true) { // Blackboard white square
if(returned == false){ // narrow
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // Zoom in
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
blackboard(startScale);
}
}
function blackboard(startScale){
background(0, 50); // blackboard
fill(255); // White squares
rectMode(CENTER); // Set the grid anchor in the center
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // Odd line
rect(x, y, startScale, startScale);
}
}
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // Even number line
rect(x, y, startScale, startScale);
}
}
}
function whiteboard(startScale){
background(255, 50); // Whiteboard
fill(0); // Black Square
rectMode(CENTER);
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // Odd line
rect(x, y, startScale, startScale);
}
}
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // Even number line
rect(x, y, startScale, startScale);
}
}
}
边栏推荐
猜你喜欢

【机器学习笔记】Regularization : Ridge Regression(岭回归)

【C语言笔记分享】字符函数和字符串函数(建议收藏)

Vscode intranet access server

关于非递归和递归分别实现求第n个斐波那契数

【p5.js学习笔记】鼠标交互事件

【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组

Jerry ac1082/1074/1090 development record
@Detailed explanation of requestmapping

三维点云处理系列----PCA

How to install PS filter plug-in
随机推荐
MySQL面试题大全(陆续更新)
Three ways to dynamically call WebService.Net
.net动态调用webservice的三种方式
leetcode系统性刷题(一)-----链表、栈、队列、堆
【 R语言—基础绘图】
软件测试前景如何?该如何进行学习呢?
C#中virtual(虚方法)的理解以及和abstract(抽象方法)的区别
软件测试需求人才越来越多,走上测试道路的人却越来越少?
.net MVC understanding
软件测试零基础小白学习需要掌握哪些技能?
【Unity FPS】教程 | 使用Unity制作第一人称角色控制器
Jerry ac1082/1074/1090 development record
软件测试工作内容太简单怎么办?
电工学自学笔记1.22
软件测试真有网上说的那么好吗?
2.2-数据类型
软件测试培训两个月骗局?培训机构到底如何选择?
Generation and use of dynamic link library (error summary)
3.2-随机数
MySQL and idea connection