10
2
We don't have a single challenge about drawing a real 3 dimensional cube, so here it goes:
Challenge
Your task is to draw a rotated, cube with perspective. It can be in a separate window or as an image.
Input
Your input is 3 separate numbers between 0 and 359.99... These represent the rotation around the x, y and z axis in degrees.
0 0 0
30 0 40
95 320 12
Output
You can either display it in a separate window or save an image. You can use any type of display (vector based, rasterized, etc.).
Edit: ASCII is allowed too, to allow golfing languages with only textual output.
The output for rasterized or ASCII graphics must be at least 50*50 (pixels for rasterization, characters for ASCII)
Additional information
The positive z axis points out from the window, the x axis is horizontal, and the y axis is vertical. Basically the OpenGL standard.
Rotations are counter-clockwise if you look at the cube in the negative direction of a specific axis, e.g looking down for the y axis.
The camera should be on the z axis at a reasonable distance from the cube in the negative z direction, the cube should be at (0;0;0). The. cube also needs to be fully visible, and take up at least 50% of the drawing frame. The camera should look in the positive z direction at the cube.
The rotations of the cube get applied in the x->y->z order.
The cube is rotated around it's center, it doesn't move.
To project a cube in 2d space, you need to divide the x and y coordinates of the cube with the distance parallel to the z-axis between the point and the camera.
Rules
Rendering libraries are allowed, but the vertices need to be defined in the code. No 3d cube model class.
1Does it have to be wireframe? – Rɪᴋᴇʀ – 2016-04-28T15:21:08.750
Care to include an algorithm for the points? – Leaky Nun – 2016-04-28T15:24:17.490
@EᴀsᴛᴇʀʟʏIʀᴋ No, I made that rule before I thought about rotating it, to prevent people from drawing a simple square, it can be solid. – Bálint – 2016-04-28T15:25:07.023
@KennyLau I'm not sure what you mean with that. – Bálint – 2016-04-28T15:25:32.117
@KennyLau google will tell you the formula. – Rɪᴋᴇʀ – 2016-04-28T15:26:07.573
@Bálint I mean the algorithm for the
projections
of the points into the 2D plane. – Leaky Nun – 2016-04-28T15:26:22.193@KennyLau It's usually done by dividing the x and y coordinates with the z. – Bálint – 2016-04-28T15:27:20.800
3What order/directions are the rotations done in? Where is the camera looking from? What kind of projection do we have to use? – flawr – 2016-04-28T19:47:59.270
@flawr camera is at 0;0;0, rotations are counter clockwise. – Bálint – 2016-04-28T20:05:40.110
Counterclockwise from what direction? And if the camera is at
0,0,0
then the rotations are going to move the cube out of the field.. – flawr – 2016-04-28T20:21:34.333@flawr I included it in the post – Bálint – 2016-04-28T20:25:02.817
6
But as I said, the rotations will not work out. As you defined it now, the cube will be moved out of the field of view if e.g. rotated around the x axis. Please use the sandbox.
– flawr – 2016-04-28T20:37:31.5106@EᴀsᴛᴇʀʟʏIʀᴋ
google will tell you the formula.
No, challenges should contain as much of the material and information needed to solve them as possible, included in the body of the post. I shouldn't have to go googling or Wikipedia-ing just to start understanding. – cat – 2016-04-28T20:47:34.667@cat true. That was a mistake. – Rɪᴋᴇʀ – 2016-04-28T20:47:58.257
related http://codegolf.stackexchange.com/q/22756/15599 . I don't think this is a duplicate, but in the light of this Ï disagree with the first line of this question
– Level River St – 2016-04-28T21:29:01.707@flawr 's points are valid. If the cube is on z axis, but is then rotated about the x or y axis, it will move off the z axis and out of view. I think you want the cube centred on the origin, so that it rotates about its centre. The camera would then be placed some distance away on the z axis. You should also specify how far, otherwise I would make the distance infinity to avoid having to deal with perspective, and simply do an orthogonal projection: x2d=x3d, y2d=y3d, z3d ignored. For a cube of sidelength 2 and hence vertices (+/-1,+/-1,+/-1) a camera at (0, 0,10 to 20) would be appropriate. – Level River St – 2016-04-28T23:00:22.760
Once that is sorted, the only other point is to mention the order in which the rotations are done, and I'd be ready to cast a reopen vote. – Level River St – 2016-04-28T23:01:52.383
@EᴀsᴛᴇʀʟʏIʀᴋ I hope it's clear now. – Bálint – 2016-04-29T05:21:28.090
There is still a contradiction here:
cube rotates about its center
+cube rotates about x,y,z axes
means cube must be at 0,0,0 (which is where you say the camera is.) As I said before, the easiest way to resolve this is to rotate about the origin and move the camera to some point on the z axis. Alternatively it would have to be '[3 numbers] represent the rotation about the z axis and axes parallel to x and y axes that pass through its centre'. You would also be wise to specify exactly how many cube sidelengths away the camera should be. – Level River St – 2016-04-29T08:43:39.000@LevelRiverSt Done – Bálint – 2016-04-29T08:51:08.747
Reminds me of this thing I did... https://codereview.stackexchange.com/questions/114767 Relevant for if you need to find some code for calculating perspective 3D points and what-not.
– Mr Public – 2016-04-29T15:15:55.837Would answers in Unity count? – Blue – 2016-04-30T02:33:29.337
@Blue If you write a code, wich generates the vertices for it. – Bálint – 2016-04-30T09:55:52.583
@Bálint lol I'd post my OpenGL answer but then you know... its OpenGL and it takes 2 pages.... – Ashwin Gupta – 2016-07-16T00:44:04.617