Code Monkey home page Code Monkey logo

cube's Introduction

Cube

Cube

Math

Rotation Matrix: For a 3-dimensional point, we have three angles to consider - $\alpha, \beta, \theta $, which represent the angles of rotation along the x, y, z axes.

So we have a vector {x, y, z}. To rotate it, we need to mutiply the 3D Rotation Matrix with it.

  1. $\alpha$ for x axis

$$ \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos{\alpha} & -\sin{\alpha} \\ 0 & \sin{\alpha} & \cos{\alpha} \\ \end{bmatrix} $$

  1. $\beta$ for y-axis

$$ \begin{bmatrix} \cos{\beta} & 0 & \sin{\beta} \\ 0 & 1 & 0 \\ -\sin{\beta} & 0 & \cos{\beta} \\ \end{bmatrix} $$

  1. $\theta$ for z-axis

$$ \begin{bmatrix} \cos{\theta} & -\sin{\theta} & 0 \\ \sin{\theta} & \cos{\theta} & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} $$

Note: we can use these 3 matrices to calculate {x', y', z'} after rotation.

How to contruct 6 surfaces for a cube

We just need to construct the first surface. Then the other 5 surfaces can be easily calculated by rotating the first surface. For example, the second surface are the rotation of 90 degrees of the first surface along the x or y axis.

How to display the cube on the screen

Perspective Projection

  1. We can imagine a virtual camera in front of the x-y axis plane, and set depth = 1 / z, z = z + distanceFromCam to simulate how we see the cube in the real world.

  2. How to map 3-dimensional points to 2-dimensional points: According to Perspective Projection, x' = x * depth, y' = y * depth. In the program, we also need to adjust the position and size of the cube:

xp := width / 2 + horizontalOffset + K1 * ooz * x * 2
yp := height / 2 + K1 * ooz * y
  1. When one depth is less that the other depth, the bigger one can cover the smaller one.

cube's People

Contributors

drafttin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.