From 2D to 3D.
in
Programming Questions
•
2 years ago
Currently studying computing at university, and with the 6 week introduction towards the course we have been given a project. To basically code an etch-a sketch.
The group did a good (To our standards) 2D sketch, however turning it into 3D caused some problems.
2D Version;
//Starting Point of etch-a-sketch
int x=400;
int y=300;
//Thickness of line
int h=2;
int w=2;
//Set colours
color Bluey = color(50,50,200);
color Redy = color(200,50,50);
color Greeny = color(50,200,50);
color Yellowy = color(200,200,50);
color Purply = color(200,50,200);
color Tealy = color(50,200,200);
color DarkGrey = color(50,50,50);
color Grey = color(200,200,200);
color Pinky = color(255,170,170);
color Orangy = color(255,69,0);
color Browny = color(139,69,19);
color CC = Greeny;
//Font
PFont font;
void setup() {
size(800, 600);
background(Redy);
font = loadFont ("AgencyFB-Bold-48.vlw");
stroke(Redy);
fill(Bluey);
rectMode(CENTER);
rect(400,25,700,50); //Top rectangle
rect(400,550,700,100); //Bot rectangle
rect(25,300,50,600); //Left rectangle
rect(775,300,50,600); //Right rectangle
//Text
textFont(font,36);
fill(Greeny);
text("1",60,40);
fill(Yellowy);
text("2",90,40);
fill(Purply);
text("3",120,40);
fill(Tealy);
text("4",150,40);
fill(DarkGrey);
text("5",180,40);
fill(Grey);
text("6",210,40);
fill(Pinky);
text("7",240,40);
fill(Orangy);
text("8",270,40);
fill(Browny);
text("9",300,40);
}
void draw() {
int dx=0;
int dy=0;
if(keyPressed) {
if(keyCode==UP) dy=-2;
if(keyCode==DOWN) dy=2;
if(keyCode==LEFT) dx=-2;
if(keyCode==RIGHT) dx=2;
if(key==054) {
h=h+1;
w=w+1;
}
if(key==056) {
w=w-1;
h=h-1;
}
if (h<2) h=2;
if (w<2) w=2;
if (h>20) h=20;
if (w>20) w=20;
if(keyCode==CONTROL) {
setup();
x=400;
y=300;
}
//The line!!!
rect(x,y,w,h);
x=x+dx;
y=y+dy;
//Line colour (the "key" numbers are ASCII characters)
stroke(CC);
fill(CC);
if(keyPressed) {
if(key==061) CC = Greeny;
if(key==062) CC = Yellowy;
if(key==063) CC = Purply;
if(key==064) CC = Tealy;
if(key==065) CC = DarkGrey;
if(key==066) CC = Grey;
if(key==067) CC = Pinky;
if(key==070) CC = Orangy;
if(key==071) CC = Browny;
}
//Stops line exiting border
if(x<61) x=61; //Left border
if(x>739) x=739; //Right border
if(y<61) y=61; //Top border
if(y>489) y=489; //Bot border
}
}
int x=400;
int y=300;
//Thickness of line
int h=2;
int w=2;
//Set colours
color Bluey = color(50,50,200);
color Redy = color(200,50,50);
color Greeny = color(50,200,50);
color Yellowy = color(200,200,50);
color Purply = color(200,50,200);
color Tealy = color(50,200,200);
color DarkGrey = color(50,50,50);
color Grey = color(200,200,200);
color Pinky = color(255,170,170);
color Orangy = color(255,69,0);
color Browny = color(139,69,19);
color CC = Greeny;
//Font
PFont font;
void setup() {
size(800, 600);
background(Redy);
font = loadFont ("AgencyFB-Bold-48.vlw");
stroke(Redy);
fill(Bluey);
rectMode(CENTER);
rect(400,25,700,50); //Top rectangle
rect(400,550,700,100); //Bot rectangle
rect(25,300,50,600); //Left rectangle
rect(775,300,50,600); //Right rectangle
//Text
textFont(font,36);
fill(Greeny);
text("1",60,40);
fill(Yellowy);
text("2",90,40);
fill(Purply);
text("3",120,40);
fill(Tealy);
text("4",150,40);
fill(DarkGrey);
text("5",180,40);
fill(Grey);
text("6",210,40);
fill(Pinky);
text("7",240,40);
fill(Orangy);
text("8",270,40);
fill(Browny);
text("9",300,40);
}
void draw() {
int dx=0;
int dy=0;
if(keyPressed) {
if(keyCode==UP) dy=-2;
if(keyCode==DOWN) dy=2;
if(keyCode==LEFT) dx=-2;
if(keyCode==RIGHT) dx=2;
if(key==054) {
h=h+1;
w=w+1;
}
if(key==056) {
w=w-1;
h=h-1;
}
if (h<2) h=2;
if (w<2) w=2;
if (h>20) h=20;
if (w>20) w=20;
if(keyCode==CONTROL) {
setup();
x=400;
y=300;
}
//The line!!!
rect(x,y,w,h);
x=x+dx;
y=y+dy;
//Line colour (the "key" numbers are ASCII characters)
stroke(CC);
fill(CC);
if(keyPressed) {
if(key==061) CC = Greeny;
if(key==062) CC = Yellowy;
if(key==063) CC = Purply;
if(key==064) CC = Tealy;
if(key==065) CC = DarkGrey;
if(key==066) CC = Grey;
if(key==067) CC = Pinky;
if(key==070) CC = Orangy;
if(key==071) CC = Browny;
}
//Stops line exiting border
if(x<61) x=61; //Left border
if(x>739) x=739; //Right border
if(y<61) y=61; //Top border
if(y>489) y=489; //Bot border
}
}
Current 3D Version;
//3D Line
float t;
PVector current_p;
PVector old_p;
int s=10;
//Image
PImage online;
//Set colours
color Bluey = color(50, 50, 200);
color Redy = color(200, 50, 50);
color Greeny = color(50, 200, 50);
color Yellowy = color(200, 200, 50);
color Purply = color(200, 50, 200);
color Tealy = color(50, 200, 200);
color DarkGrey = color(50, 50, 50);
color Grey = color(200, 200, 200);
color Pinky = color(255, 170, 170);
color Orangy = color(255, 69, 0);
color Browny = color(139, 69, 19);
color CC = Greeny;
//Font
PFont font;
void setup(){
size(800,600,P3D);
background(Redy);
font = loadFont ("AgencyFB-Bold-48.vlw");
stroke(Bluey);
fill(Bluey);
rectMode(CENTER);
rect(400, 25, 700, 50); //Top rectangle
rect(400, 540, 700, 120); //Bot rectangle
rect(25, 300, 50, 600); //Left rectangle
rect(775, 300, 50, 600); //Right rectangle
//Text
textFont(font, 36);
fill(Greeny);
text("1", 60, 40);
fill(Yellowy);
text("2", 90, 40);
fill(Purply);
text("3", 120, 40);
fill(Tealy);
text("4", 150, 40);
fill(DarkGrey);
text("5", 180, 40);
fill(Grey);
text("6", 210, 40);
fill(Pinky);
text("7", 240, 40);
fill(Orangy);
text("8", 270, 40);
fill(Browny);
text("9", 300, 40);
fill(255);
text(">", 720, 40);
text("<", 690, 40);
text("P.T.T LTD", 635, 550);
online = loadImage("http://i53.tinypic.com/34hyjbt
.jpg", "jpg");
current_p=new PVector(400,300,0);
old_p=new PVector(400,300,0);
}
void clear(){
background(Redy);
stroke(Bluey);
fill(Bluey);
rectMode(CENTER);
rect(400, 25, 700, 50); //Top rectangle
rect(400, 540, 700, 120); //Bot rectangle
rect(25, 300, 50, 600); //Left rectangle
rect(775, 300, 50, 600); //Right rectangle
//Text
textFont(font, 36);
fill(Greeny);
text("1", 60, 40);
fill(Yellowy);
text("2", 90, 40);
fill(Purply);
text("3", 120, 40);
fill(Tealy);
text("4", 150, 40);
fill(DarkGrey);
text("5", 180, 40);
fill(Grey);
text("6", 210, 40);
fill(Pinky);
text("7", 240, 40);
fill(Orangy);
text("8", 270, 40);
fill(Browny);
text("9", 300, 40);
fill(255);
text(">", 720, 40);
text("<", 690, 40);
text("P.T.T LTD", 635, 550);
}
void keyPressed(){
if (key=='w') {
old_p.y=current_p.y;
current_p.y-=2.0;
}
if (key=='s') {
old_p.y=current_p.y;
current_p.y+=2.0;
}
if (key=='a') {
old_p.x=current_p.x;
current_p.x-=2.0;
}
if (key=='d') {
old_p.x=current_p.x;
current_p.x+=2.0;
}
if (key=='e') {
old_p.z=current_p.z;
current_p.z-=5.0;
}
if (key=='q') {
old_p.z=current_p.z;
current_p.z+=5.0;
}
if (key==061) CC = Greeny;
if (key==062) CC = Yellowy;
if (key==063) CC = Purply;
if (key==064) CC = Tealy;
if (key==065) CC = DarkGrey;
if (key==066) CC = Grey;
if (key==067) CC = Pinky;
if (key==070) CC = Orangy;
if (key==071) CC = Browny;
if (key==',') {
s=s-1;
if (s<4) s=4;
}
if (key=='.') {
s=s+1;
if (s>20) s=20;
}
}
void draw(){
//Image
image(online,60,490);
translate(current_p.x,current_
p.y,current_p.z);
fill(CC);
noStroke();
rect(0,0,s,s);
t=t+0.01;
}
float t;
PVector current_p;
PVector old_p;
int s=10;
//Image
PImage online;
//Set colours
color Bluey = color(50, 50, 200);
color Redy = color(200, 50, 50);
color Greeny = color(50, 200, 50);
color Yellowy = color(200, 200, 50);
color Purply = color(200, 50, 200);
color Tealy = color(50, 200, 200);
color DarkGrey = color(50, 50, 50);
color Grey = color(200, 200, 200);
color Pinky = color(255, 170, 170);
color Orangy = color(255, 69, 0);
color Browny = color(139, 69, 19);
color CC = Greeny;
//Font
PFont font;
void setup(){
size(800,600,P3D);
background(Redy);
font = loadFont ("AgencyFB-Bold-48.vlw");
stroke(Bluey);
fill(Bluey);
rectMode(CENTER);
rect(400, 25, 700, 50); //Top rectangle
rect(400, 540, 700, 120); //Bot rectangle
rect(25, 300, 50, 600); //Left rectangle
rect(775, 300, 50, 600); //Right rectangle
//Text
textFont(font, 36);
fill(Greeny);
text("1", 60, 40);
fill(Yellowy);
text("2", 90, 40);
fill(Purply);
text("3", 120, 40);
fill(Tealy);
text("4", 150, 40);
fill(DarkGrey);
text("5", 180, 40);
fill(Grey);
text("6", 210, 40);
fill(Pinky);
text("7", 240, 40);
fill(Orangy);
text("8", 270, 40);
fill(Browny);
text("9", 300, 40);
fill(255);
text(">", 720, 40);
text("<", 690, 40);
text("P.T.T LTD", 635, 550);
online = loadImage("http://i53.tinypic.com/34hyjbt
current_p=new PVector(400,300,0);
old_p=new PVector(400,300,0);
}
void clear(){
background(Redy);
stroke(Bluey);
fill(Bluey);
rectMode(CENTER);
rect(400, 25, 700, 50); //Top rectangle
rect(400, 540, 700, 120); //Bot rectangle
rect(25, 300, 50, 600); //Left rectangle
rect(775, 300, 50, 600); //Right rectangle
//Text
textFont(font, 36);
fill(Greeny);
text("1", 60, 40);
fill(Yellowy);
text("2", 90, 40);
fill(Purply);
text("3", 120, 40);
fill(Tealy);
text("4", 150, 40);
fill(DarkGrey);
text("5", 180, 40);
fill(Grey);
text("6", 210, 40);
fill(Pinky);
text("7", 240, 40);
fill(Orangy);
text("8", 270, 40);
fill(Browny);
text("9", 300, 40);
fill(255);
text(">", 720, 40);
text("<", 690, 40);
text("P.T.T LTD", 635, 550);
}
void keyPressed(){
if (key=='w') {
old_p.y=current_p.y;
current_p.y-=2.0;
}
if (key=='s') {
old_p.y=current_p.y;
current_p.y+=2.0;
}
if (key=='a') {
old_p.x=current_p.x;
current_p.x-=2.0;
}
if (key=='d') {
old_p.x=current_p.x;
current_p.x+=2.0;
}
if (key=='e') {
old_p.z=current_p.z;
current_p.z-=5.0;
}
if (key=='q') {
old_p.z=current_p.z;
current_p.z+=5.0;
}
if (key==061) CC = Greeny;
if (key==062) CC = Yellowy;
if (key==063) CC = Purply;
if (key==064) CC = Tealy;
if (key==065) CC = DarkGrey;
if (key==066) CC = Grey;
if (key==067) CC = Pinky;
if (key==070) CC = Orangy;
if (key==071) CC = Browny;
if (key==',') {
s=s-1;
if (s<4) s=4;
}
if (key=='.') {
s=s+1;
if (s>20) s=20;
}
}
void draw(){
//Image
image(online,60,490);
translate(current_p.x,current_
fill(CC);
noStroke();
rect(0,0,s,s);
t=t+0.01;
}
[Font wont load properly if you don't have it].
From the transition to 3D, the 'reset' code doesn't work anymore. Neither does the borders.
1