c# - Console 3D Cube Rotating -
what algorithm creating "3d" cube in console , rotating (dice roll style)?
creative answers appreciated.
i don't know if qualifies "algorithm" you'd looking for, full 3d calculations.
example code drawing cube wireframe (i'm using vector/matrix classes opentk, them xna or other library well)
using system; using system.linq; using opentk; class cubes { static void main() { var resolution = 25; var points = in enumerable.range(1, 8) select new vector3(i / 4 % 2 * 2 - 1, / 2 % 2 * 2 - 1, % 2 * 2 - 1); var lines = in points b in points (a - b).length == 2 // adjacent points && a.x + a.y + a.z > b.x + b.y + b.z // select each pair once select new { a, b }; var t = 0f; while (true) { t += .1f; var projection = matrix4.createperspectivefieldofview(.8f, 1, .01f, 100f); var view = matrix4.lookat(2 * new vector3((float)math.sin(t), .5f, (float)math.cos(t)), vector3.zero, vector3.unity); console.clear(); foreach (var line in lines) { (int = 0; < resolution; i++) { var point = (1f / resolution) * (i * line.a + (resolution - 1 - i) * line.b); // interpolate point between 2 corners var p1 = 5 * vector3.transform(point, view * projection) + new vector3(30, 20, 0); console.setcursorposition((int)p1.x, (int)p1.y); console.write("#"); } } console.readkey(); } } }
sample frames output:
# ###### #### # ## #### # ## #### # ## ### # ## ## # ### ### # ## # ## # ## # ## # ### # ## # ### # # ## # ### # # ## ##### # # ## ### # # ##### # # # # # # # # # # # # # # # # # # # # # # # # # # # # ##### # # ### ## # # ##### ## # # ### # ## # # ### # ## # ### # ## # ## # ## # ## # ### ### # ## ## # ### ## # #### ## # #### ## # #### ###### # # ###### ## # ### ## # ### ## # #### ## # #### ## # ## ## # ### # # ## # ## # ## # # #### # ## # # #### # # # # #### ## # # ##### ## # # # #### # # # # # # # # # # # # # # # # # # # # # # # # # # #### # # # ## ##### # # ## #### # # # # #### # # ## # #### # # ## # ## # ## # # ### # ## ## # ## #### # ## #### # ## ### # ## ### # ## ###### # # ############## # ########## ## ## ## ## ### ### # # # # # # # # ## # ## # # # # # ## # # # ############## # # # # ########## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ########## # # # # ############## # # # ## # # # # # ## # ## # # # # # # # # ### ### ## ## ## ## ########## # ############## #
Comments
Post a Comment