2015-07-02 7 views
-1

У меня есть упражнение, которое требует поворота куба, созданного только с 2D-линиями вокруг оси x или y.Как повернуть вокруг оси x и y в 2D?

Вот что я должен сделать:

Цель этого проекта заключается в имитации 3dimensional отображения простого куба, используя только 2dimensional рисунки строки.

Это возможно потому, что для любой точки зрения, есть только 3 возможных конфигураций, когда вы смотрите на кубе и отобразить только его видимые лица:

-3 граней куба видны (левое изображение), что в общем случае

-только-лица видно (средний рисунок)

-только 1 лицо видно (правая картинка - пунктирные линии не должны появляться в вашем приложении)

enter image description here

Вы должны разработать программу, которая позволяет пользователю перемещаться вокруг куба с центром в середине экрана, с применением вращений, связанных с одной из своих 3-м осям (управляемый с помощью мыши или клавиатуры, используя например, «x» или «X», чтобы вращаться вокруг оси X).

Ваша программа должна быть разработана с использованием обработки: https://processing.org/ Этот язык очень похож на Java, вы можете найти много примеров и учебных пособий, например, как рисовать линии в 2D: https://processing.org/tutorials/drawing/.

Хотя вы можете взглянуть на некоторые довольно похожие проекты (например, https://processing.org/examples/rgbcube.html), пожалуйста, обратите внимание, что для этого проекта вам не разрешено использовать примитивы 3D-чертежа.

Если у вас есть время вы можете попытаться улучшить свой проект, добавляя новые функции:

-zoom или отдалить

-DISplay несколько кубов

рассмотрят другие виды платонических твердых веществ (https://en.wikipedia.org/wiki/Platonic_solid), для которого вы также можете определить максимальное количество видимых граней

Я прочитал много сообщений о 2D-преобразовании и с использованием преобразования m но я не могу понять, как это сделать в любом случае.

Это то, что я сделал до сих пор:

boolean locked = false; 
float a = 0.2; 
float s = 80; 
float x0 = s; 
float y0 = -s; 
float x1 = s; 
float y1 = s; 
float x2 = -s; 
float y2 = -s; 
float x3 = -s; 
float y3 = s; 
float x0a, y0a, x1a, y1a, x2a, y2a, x3a, y3a; 

void setup() { 
    size(480, 360); 
    noStroke(); 
    translate(width/2, height/2); 
    line(x0,y0,x1,y1); 
    line(x2,y2,x3,y3); 
    line(x0,y0,x2,y2); 
    line(x3,y3,x1,y1); 
} 

void draw() { 
    update(); 
    background(0); 
    stroke(255);  
    pushMatrix(); 
    translate(width/2, height/2); 
    line(x0,y0,x1,y1); 
    line(x2,y2,x3,y3); 
    line(x0,y0,x2,y2); 
    line(x3,y3,x1,y1); 
    popMatrix(); 
} 

void update(){ 
    if (locked && mouseButton==LEFT){ 
    y0a = y0*cos(a); 
    y0 = y0a; 
    y1a = y1*cos(a); 
    y1 = y1a; 
    y2a = y2*cos(a); 
    y2 = y2a; 
    y3a = y3*cos(a); 
    y3 = y3a; 
    } 
    if (locked && mouseButton==RIGHT){ 
    y0a = y0*cos(a); 
    y0 = y0a; 
    y1a = y1*cos(a); 
    y1 = y1a; 
    y2a = y2*cos(a); 
    y2 = y2a; 
    y3a = y3*cos(a); 
    y3 = y3a; 
    } 
} 

void mousePressed(){ 
    if (mouseButton == LEFT) 
    locked=true; 
    if (mouseButton == RIGHT) 
    locked=true; 
} 

void mouseReleased(){ 
    locked = false; 
} 

void keyPressed() { 
    if (key == 'q'){ 
    x0a = x0*cos(a)-y0*sin(a); 
    y0a = x0*sin(a)+y0*cos(a); 
    x0 = x0a; 
    y0 = y0a; 
    x1a = x1*cos(a)-y1*sin(a); 
    y1a = x1*sin(a)+y1*cos(a); 
    x1 = x1a; 
    y1 = y1a; 
    x2a = x2*cos(a)-y2*sin(a); 
    y2a = x2*sin(a)+y2*cos(a); 
    x2 = x2a; 
    y2 = y2a; 
    x3a = x3*cos(a)-y3*sin(a); 
    y3a = x3*sin(a)+y3*cos(a); 
    x3 = x3a; 
    y3 = y3a; 
    } 
    if (key == 'w'){ 
    x0a = x0*cos(a)+y0*sin(a); 
    y0a = x0*-sin(a)+y0*cos(a); 
    x0 = x0a; 
    y0 = y0a; 
    x1a = x1*cos(a)+y1*sin(a); 
    y1a = x1*-sin(a)+y1*cos(a); 
    x1 = x1a; 
    y1 = y1a; 
    x2a = x2*cos(a)+y2*sin(a); 
    y2a = x2*-sin(a)+y2*cos(a); 
    x2 = x2a; 
    y2 = y2a; 
    x3a = x3*cos(a)+y3*sin(a); 
    y3a = x3*-sin(a)+y3*cos(a); 
    x3 = x3a; 
    y3 = y3a; 
    } 
    if (key == 'a'){ 
    x0a = x0*cos(a); 
    x0 = x0a; 
    x1a = x1*cos(a); 
    x1 = x1a; 
    x2a = x2*cos(a); 
    x2 = x2a; 
    x3a = x3*cos(a); 
    x3 = x3a; 
    } 
    if (key == 's'){ 
    x0a = x0*cos(a); 
    x0 = x0a; 
    x1a = x1*cos(a); 
    x1 = x1a; 
    x2a = x2*cos(a); 
    x2 = x2a; 
    x3a = x3*cos(a); 
    x3 = x3a; 
    } 
    if (key == 'z'){ 
    y0a = y0*cos(a); 
    y0 = y0a; 
    y1a = y1*cos(a); 
    y1 = y1a; 
    y2a = y2*cos(a); 
    y2 = y2a; 
    y3a = y3*cos(a); 
    y3 = y3a; 
    } 
    if (key == 'x'){ 
    y0a = y0*cos(a); 
    y0 = y0a; 
    y1a = y1*cos(a); 
    y1 = y1a; 
    y2a = y2*cos(a); 
    y2 = y2a; 
    y3a = y3*cos(a); 
    y3 = y3a; 
    }  
} 
+0

Извините, но мы не можем просто сделать домашнее задание для вас. Почему бы вам не опубликовать то, что вы пробовали, в качестве [MCVE] (http://stackoverflow.com/help/mcve)? Можете ли вы повернуть точку? Как насчет линии?Где именно вы застряли? –

ответ

0

Я думаю, что мне это удалось! Вот код, который реализует 3-х сторон куба

float s = 80; 
Square sq1 = new Square(s, -s, s, s, s, s, -s, -s, s, -s, s, s); 
Square sq2 = new Square(s, -s, -s, s, s, -s, -s, -s, -s, -s, s, -s); 
Square sq3 = new Square(-s, -s, s, -s, -s, -s, s, -s, s, s, -s, -s); 

void setup(){ 
    size(480, 360); 
    noStroke(); 
    translate(width/2, height/2); 
} 

void draw() { 
    background(0); 
    stroke(255); 
    translate(width/2, height/2); 
    sq1.updateSquare(); 
    sq1.drawSquare(width/2, height/2); 
    sq2.updateSquare(); 
    sq2.drawSquare(width/2, height/2); 
    sq3.updateSquare(); 
    sq3.drawSquare(width/2, height/2); 
} 

class Square { 
    float theta = 0.2; 
    float x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3; 

    Square(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3){ 
    this.x0 = x0; 
    this.y0 = y0; 
    this.z0 = z0; 
    this.x1 = x1; 
    this.y1 = y1; 
    this.z1 = z1; 
    this.x2 = x2; 
    this.y2 = y2; 
    this.z2 = z2; 
    this.x3 = x3; 
    this.y3 = y3; 
    this.z3 = z3; 
    } 

    void updateSquare(){ 
    float tmpX, tmpY, tmpZ;  
    if (keyPressed && key=='z'){ 
     tmpX = this.x0*cos(theta)-this.y0*sin(theta); 
     tmpY = this.x0*sin(theta)+this.y0*cos(theta); 
     this.x0 = tmpX; 
     this.y0 = tmpY; 

     tmpX = this.x1*cos(theta)-this.y1*sin(theta); 
     tmpY = this.x1*sin(theta)+this.y1*cos(theta); 
     this.x1 = tmpX; 
     this.y1 = tmpY; 

     tmpX = this.x2*cos(theta)-this.y2*sin(theta); 
     tmpY = this.x2*sin(theta)+this.y2*cos(theta); 
     this.x2 = tmpX; 
     this.y2 = tmpY; 

     tmpX = this.x3*cos(theta)-this.y3*sin(theta); 
     tmpY = this.x3*sin(theta)+this.y3*cos(theta); 
     this.x3 = tmpX; 
     this.y3 = tmpY; 
    } 
    if (keyPressed && key=='x'){ 
     tmpY = this.y0*cos(theta)-this.z0*sin(theta); 
     tmpZ = this.y0*sin(theta)+this.z0*cos(theta); 
     this.y0 = tmpY; 
     this.z0 = tmpZ;  

     tmpY = this.y1*cos(theta)-this.z1*sin(theta); 
     tmpZ = this.y1*sin(theta)+this.z1*cos(theta); 
     this.y1 = tmpY; 
     this.z1 = tmpZ; 

     tmpY = this.y2*cos(theta)-this.z2*sin(theta); 
     tmpZ = this.y2*sin(theta)+this.z2*cos(theta); 
     this.y2 = tmpY; 
     this.z2 = tmpZ; 

     tmpY = this.y3*cos(theta)-this.z3*sin(theta); 
     tmpZ = this.y3*sin(theta)+this.z3*cos(theta); 
     this.y3 = tmpY; 
     this.z3 = tmpZ;    
    } 
    if (keyPressed && key=='y'){ 
     tmpX = this.x0*cos(theta)+this.z0*sin(theta); 
     tmpZ = this.x0*-sin(theta)+this.z0*cos(theta); 
     this.x0 = tmpX; 
     this.z0 = tmpZ; 

     tmpX = this.x1*cos(theta)+this.z1*sin(theta); 
     tmpZ = this.x1*-sin(theta)+this.z1*cos(theta); 
     this.x1 = tmpX; 
     this.z1 = tmpZ; 

     tmpX = this.x2*cos(theta)+this.z2*sin(theta); 
     tmpZ = this.x2*-sin(theta)+this.z2*cos(theta); 
     this.x2 = tmpX; 
     this.z2 = tmpZ; 

     tmpX = this.x3*cos(theta)+this.z3*sin(theta); 
     tmpZ = this.x3*-sin(theta)+this.z3*cos(theta); 
     this.x3 = tmpX; 
     this.z3 = tmpZ;  
    } 
    } 

    void drawSquare(float vW, float vH){  
    line(this.x0, this.y0, this.x1, this.y1); 
    line(this.x2, this.y2, this.x3, this.y3); 
    line(this.x0, this.y0, this.x2, this.y2); 
    line(this.x3, this.y3, this.x1, this.y1); 
    } 
} 

Теперь мне нужно помочь прятать строки, которые должны быть на спине! Может ли кто-нибудь помочь?

Смежные вопросы