----------------------------------------
將兩點連起來
void setup(){
size(300,300);
}
void draw(){
background (#002966); //背景顏色
stroke(#b7efff); //線條顏色
line(100,150, 200,150);
fill(#b7efff); //填顏色(填充)
ellipse(100,150,13,13);
ellipse(200,150,13,13);
}
----------------------------------
點變成可拖曳
void setup(){
size(300,300);
}
float x=100, y=150;
void draw(){
background (#002966);
stroke(#b7efff);
line(x,y, 200,150);
fill(#b7efff);
ellipse(x,y,13,13);
ellipse(200,150,13,13);
}
void mouseDragged(){
x= mouseX;
y= mouseY;
}
-------------------------------------------
拖曳點可彈回去
原理 :
void setup(){
size(300,300);
}
float x=100, y=150;
void draw(){
background (#002966);
stroke(#b7efff);
line(x,y, 200,150);
fill(#b7efff);
ellipse(x,y,13,13);
ellipse(200,150,13,13);
if(! mousePressed){
float dx= x-200;
float dy= y-150;
float len=sqrt(dx*dx+dy*dy);
x -= dx*(len-100)/len;
y -= dy*(len-100)/len;
}
}
void mouseDragged(){
x= mouseX;
y= mouseY;
}
-----------------------------------
滑鼠拖曳點
放開拖曳的點,回到原先拖曳前的座標
------------------------------------------------------
一次只走全部長度的 1/10,緩慢彈回
void setup(){
size(300,300);
}
float x=100, y=150;
void draw(){
background (#002966);
stroke(#b7efff);
line(x,y, 200,150);
fill(#b7efff);
ellipse(x,y,13,13);
ellipse(200,150,13,13);
if(! mousePressed){
float dx= x-200;
float dy= y-150;
float len=sqrt(dx*dx+dy*dy);
x -= dx*(len-100)/len*0.1;
y -= dy*(len-100)/len*0.1;
}
}
void mouseDragged(){
x= mouseX;
y= mouseY;
}
---------------------------------------------
換項目 : 餵魚遊戲
魚(圓形)會跟著滑鼠移動,每次追 1/10
void setup(){
size(400,300);
}
float x=100, y=150;
void draw(){
background(#002966);
ellipse(x,y, 50,50);
float dx= x-mouseX;
float dy= y-mouseY;
float len= sqrt(dx*dx+dy*dy);
x -= dx*0.1;
y -= dy*0.1;
}
---------------------------------------------
跟滑鼠保持 100 的距離,不要黏太緊
(做法類似剛剛的第一個應用)
void setup(){
size(400,300);
}
float x=100, y=150;
void draw(){
background(#002966);
ellipse(x,y, 50,50);
float dx= x-mouseX;
float dy= y-mouseY;
float len= sqrt(dx*dx+dy*dy);
x -= dx*(len-100)/len*0.1;
y -= dy*(len-100)/len*0.1;
}
--------------------------------------------
回到虎克彈簧 : 接續前面進度
做出真正有彈性的彈簧
void setup(){
size(300,300);
}
float x=100, y=150;
float vx=0, vy=0;
void draw(){
background (#002966);
stroke(#b7efff);
line(x,y, 200,150);
fill(#b7efff);
ellipse(x,y,13,13);
ellipse(200,150,13,13);
if(! mousePressed){
float dx= x-200;
float dy= y-150;
float len=sqrt(dx*dx+dy*dy);
float f=(len-100);
dx/=len;
dy/=len;
vx -= f*dx*0.1; //0.1 fast, 0.01 slow
vy -= f*dy*0.1;
x += vx;
y += vy;
}
}
void mouseDragged(){
x= mouseX;
y= mouseY;
}
-----------------------------------------------
印檔案
#include <stdio.h>
int main()
{
FILE * fout=fopen("my_first_file.txt", "w+");
printf("Hello World\n");
fprintf(fout,"Hellp World\n");
}
會產生 .txt 檔案,印出 Hello World
------------------------------------------------
讀檔案
#include <stdio.h>
char line[10000];
int main()
{
FILE * fin=fopen("my_first_file.txt", "r");
fscanf(fin, "%s",line);
printf("=%s=",line);
}

沒有留言:
張貼留言