Draw a cuboid: Difference between revisions

m
→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work
(Added Forth entry)
m (→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work)
 
(12 intermediate revisions by 7 users not shown)
Line 943:
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZRNj9owEIbv/hWv2BsoEUkIUKk5rNRjT5V6inKgxFALsFdJVGJ+fTX+2Dhp2C5CTBg/8854PI5UNS+rskKBEiWiBFFK3+r9T+KfQ793h3hAB7BnK8brc5iKFghIsTF2g8zYzGnk2Bq7xd7YPXbG7pA74dzFb13czunsTboXsLdGHdEeD1eOE2LEDMBJNRAokKBTuHIJ6TpAi2699uuZcwIWE1VZV1gWOLkFIxmz2KZqVHfoOA7y3NOP9jlbIXsUZMwauY6qJddRte+uVkg9UNpReqD0Z+onVV9r4p2PwJl550ChQI+lrSnCA0tTscN0EJuGgg8fsjLRQciAo4C2mB6U9QTLqkFNY2UiPEa9NbusywqZbXPdHO6+tccrPzT0cFN/OFKk9NzxvsPitWnUHReuW2qVPZuFQVHgC33MH2pmlHymteIUlvwVt2E6bnMN9upiPCykRKc0n9PfkiGn8fhzuolgJOtS9OT0RBpkFj1WLgcAfg2E0v8KJU+Fhj1Q5XLcrnp0iz7a1szOZF1KUUE14zqtewijEU98tweBcc00trNQOlFKp1A6ozQLTZTM+PUJVsjXlJvsCLgKySmfBdIJEE/fJzTCrXhwZMy+wJKcudfLJscmZ3QJmJI038x288L1rwun62pH/+fbwqm6wLU9RTMLU/ibust/8Og5/52fugkfJVg/5X+I8+9pgOPNns12Ysb+Ag== Run it]
<syntaxhighlight>
node[][] = [ [ -1 -2 -2 ] [ -1 -2 1 ] [ -1 2 -2 ] [ -1 2 1 ] [ 1 -2 -2 ] [ 1 -2 1 ] [ 1 2 -2 ] [ 1 2 1 ] ]
edge[][] = [ [ 1 2 ] [ 2 4 ] [ 4 3 ] [ 3 1 ] [ 5 6 ] [ 6 8 ] [ 8 7 ] [ 7 5 ] [ 1 5 ] [ 2 6 ] [ 3 7 ] [ 4 8 ] ]
#
proc scale f . .
for i = 1 to len node[][]
for d = 1 to 3
node[i][d] *= f
.
.
.
proc rotate angx angy . .
sinx = sin angx
cosx = cos angx
siny = sin angy
cosy = cos angy
for i = 1 to len node[][]
x = node[i][1]
z = node[i][3]
node[i][1] = x * cosx - z * sinx
y = node[i][2]
z = z * cosx + x * sinx
node[i][2] = y * cosy - z * siny
node[i][3] = z * cosy + y * siny
.
.
len nd[] 3
proc draw . .
clear
move 2 2
text "Arrow keys to rotate"
m = 99999
mi = -1
for i = 1 to len node[][]
if node[i][3] < m
m = node[i][3]
mi = i
.
.
ix = 1
for i = 1 to len edge[][]
if edge[i][1] = mi
nd[ix] = edge[i][2]
ix += 1
elif edge[i][2] = mi
nd[ix] = edge[i][1]
ix += 1
.
.
for ni = 1 to len nd[]
for i = 1 to len edge[][]
if edge[i][1] = nd[ni] or edge[i][2] = nd[ni]
x1 = node[edge[i][1]][1]
y1 = node[edge[i][1]][2]
x2 = node[edge[i][2]][1]
y2 = node[edge[i][2]][2]
move x1 + 50 y1 + 50
line x2 + 50 y2 + 50
.
.
.
.
textsize 3
scale 15
rotate 45 45
draw
on key
if keybkey = "ArrowUp"
rotate 0 1
elif keybkey = "ArrowDown"
rotate 0 -1
elif keybkey = "ArrowLeft"
rotate -1 0
elif keybkey = "ArrowRight"
rotate 1 0
.
draw
.
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 1,036 ⟶ 1,118:
+-----+-----+-----+-----+
</pre>
 
=={{header|Evaldraw}}==
 
=== Solid and stippled lines ===
{{trans|XPL0}}
 
Based on the XPL0 solution, but provides its own stippled line drawing routine, since evaldraw only supports solid lines out of the box.
 
[[File:Evaldraw line cuboid.gif|thumb|alt=Rotating cuboid drawn with solid visible lines and stippled hidden lines|Rotating cuboid. Visible lines solid, hidden edges drawn with stippled lines]]
 
<syntaxhighlight lang="C">
static points_x[8] = {-2.0, +2.0, +2.0, -2.0, -2.0, +2.0, +2.0, -2.0};
static points_y[8] = {-1.5, -1.5, +1.5, +1.5, -1.5, -1.5, +1.5, +1.5};
static points_z[8] = {-1.0, -1.0, -1.0, -1.0, +1.0, +1.0, +1.0, +1.0};
static segment[2*12] = {0,1, 1,2, 2,3, 3,0, 4,5, 5,6, 6,7, 7,4, 0,4, 1,5, 2,6, 3,7};
static size=50, sz=0.008, sx=-0.013; // drawing size and tumbling speeds
() {
mind = 0.0; si=0;
for(i=0; i<8; i++) {
if (points_z[i] < mind) { mind=points_z[i]; si=i;}
}
cls(0); // Clear Color Buffer
for(i=0; i<2*12-1; i+=2) {
j=segment[i];
x0 = points_x[j]*size + xres/2;
y0 = points_y[j]*size + yres/2;
k=segment[i+1];
x1 = points_x[k]*size + xres/2;
y1 = points_y[k]*size + yres/2;
if (j!=si && k!=si) {
setcol(255,0,0);
moveto(x0,y0); lineto(x1,y1);
} else {
setcol(255,255,0);
drawLineStipple(x0,y0,x1,y1,8);
}
}
sleep(16); // Sleep for 16 millis so cube tumbles slowly
for(i=0; i<8; i++) {
points_x[i] = points_x[i] + points_y[i]*Sz; //rotate vertices in X-Y plane
points_y[i] = points_y[i] - points_x[i]*Sz;
points_y[i] = points_y[i] + points_z[i]*Sx; //rotate vertices in Y-Z plane
points_z[i] = points_z[i] - points_y[i]*Sx;
}
}
 
drawLineStipple(x1,y1,x2,y2,stipple_dist) {
xdist = x1-x2; ydist=y1-y2;
stipple_dist2 = stipple_dist / 2;
if ( abs(xdist^2 + ydist^2) < stipple_dist2^2 ) return;
if(xdist < 0) xdist=-xdist;
if(ydist < 0) ydist=-ydist;
mv=0; if(ydist > xdist) mv = ydist; else mv = xdist;
x = x1; y = y1;
stepx = xdist/mv; if(x1 > x2) stepx = -stepx;
stepy = ydist/mv; if(y1 > y2) stepy = -stepy;
for(nc=0; nc<int(mv); nc++) {
if( nc % stipple_dist < stipple_dist2) setpix(x,y);
x+=stepx; y+=stepy;
}
}
</syntaxhighlight>
 
=== OpenGL-like filled polygons ===
 
[[File:Evaldraw textured cuboid.png|thumb|alt=Cuboid, 2 units wide, 3 units high, 4 units deep|Textured cuboid. Each face has its own color. Rotate with mouse position.]]
 
<syntaxhighlight lang="C">// We can define our own vec3 struct
struct vec3{x,y,z;}
 
// Static allows for globals
static modelMatrix[9];
() {
cls(0,0,32); // clear screen
clz(1e32); // clear depth buffer
setcam(0,0,-10,0,0); // set camera some units back
// create two local arrays to hold rotation matrices
double roty[9], rotz[9];
// we can access current mouse position and screen size
rotateZ( rotz, mousy/yres*2*pi );
rotateY( roty, mousx/xres*2*pi );
// evaldraw does support some GL-like drawing
// modes, but any transformations must be done by hand
// Here we use a global model matrix that
// transforms vertices created by the myVertex function
mult(modelMatrix, roty, rotz);
drawcuboid(0,0,0,2,3,4);
sleep(10);
}
 
drawcuboid(x,y,z,sx,sy,sz) {
glBegin(GL_QUADS);
setcol(192,0,0);
glTexCoord(0,0); myVertex(x-sx,y-sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z-sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z-sz);
setcol(0,192,0);
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x-sx,y-sy,z-sz);
glTexCoord(1,1); myVertex(x-sx,y+sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz);
setcol(0,0,192);
glTexCoord(0,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x-sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x+sx,y+sy,z+sz);
setcol(192,192,0);
glTexCoord(0,0); myVertex(x+sx,y-sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x+sx,y+sy,z-sz);
 
setcol(192,0,192);
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x+sx,y-sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y-sy,z-sz);
 
setcol(0,192,192);
glTexCoord(0,0); myVertex(x-sx,y+sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y+sy,z-sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz);
glEnd();
}
 
myVertex(x,y,z) {
// Initialize a struct value
vec3 v = {x,y,z};
// Apply global model matrix transformation
transformPoint(v, modelMatrix);
// Submit the vertex to draw list
glVertex(v.x, v.y, v.z);
}
 
rotateX(m[9], r) { // structs and arrays are pass-by-ref
c = cos(r); s=sin(r);
m[0] = 1; m[1] = 0; m[2] = 0;
m[3] = 0; m[4] = c; m[5] = -s;
m[6] = 0; m[7] = s; m[8] = c;
}
 
rotateY(m[9], r) {
c = cos(r); s=sin(r);
m[0] = c; m[1] = 0; m[2] = s;
m[3] = 0; m[4] = 1; m[5] = 0;
m[6] = -s; m[7] = 0; m[8] = c;
}
 
rotateZ(m[9], r) {
c = cos(r); s=sin(r);
m[0] = c; m[1] = -s; m[2] = 0;
m[3] = s; m[4] = c; m[5] = 0;
m[6] = 0; m[7] = 0; m[8] = 1;
}
 
transformPoint(vec3 v, m[9]) {
x2 = v.x * m[0] + v.y * m[1] + v.z * m[2];
y2 = v.x * m[3] + v.y * m[4] + v.z * m[5];
z2 = v.x * m[6] + v.y * m[7] + v.z * m[8];
// Mutate the struct v with new values
v.x=x2; v.y=y2; v.z=z2;
}
 
mult(c[9],a[9],b[9]) { // C = AB
// multiply a row in A with a column in B
for(i=0; i<3; i++)
for(j=0; j<3; j++) {
sum = 0.0;
for(k=0; k<3; k++) {
sum += A[k*3+i] * B[k*3+j];
}
C[i*3+j] = sum;
}
}</syntaxhighlight>
 
=={{header|Factor}}==
Line 1,067 ⟶ 1,338:
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
=== ASCII lines ===
{{trans|PicoLisp}}
 
Line 1,135 ⟶ 1,407:
</pre>
 
=== ASCII faces ===
Inspired from X86 Assembly
 
<syntaxhighlight lang="Forth">
 
: hline ( char len )
0 ?do dup emit loop drop ;
: vline ( char len )
0 ?do dup emit -1 1 at-deltaxy loop drop ;
: cuboid { dz dy dx -- }
page
dy 0 ?do dy i - i at-xy '# dx hline loop
dz 0 ?do 0 dy i + at-xy '+ dx hline loop
dy 0 ?do dx i + dy i - at-xy '/ dz vline loop
;</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="Forth">4 3 2 cuboid</syntaxhighlight>
<pre> ##
##/
##//
++///
++///
++// ok
++/
</pre>
<syntaxhighlight lang="Forth">5 5 5 cuboid</syntaxhighlight>
<pre> #####
#####/
#####//
#####///
#####////
+++++/////
+++++//// ok
+++++///
+++++//
+++++/
</pre>
 
=={{header|FreeBASIC}}==
Line 1,228 ⟶ 1,535:
println["done."]</syntaxhighlight>
 
=={{header|FutureBasic}}==
This code compiles into a macOS application that allows 360-degree mouse rotation of the cuboid. All six faces of the cuboid have different colors.
<syntaxhighlight lang="futuebasic">
include "Tlbx SceneKit.incl"
 
_window = 1
begin enum output 1
_sceneView
end enum
 
local fn Cuboid as SCNSceneRef
SCNSceneRef scene = fn SCNSceneInit
SCNNodeRef rootNode = fn SCNSceneRootNode( scene )
SCNCameraRef camera = fn SCNCameraInit
SCNNodeRef cameraNode = fn SCNNodeInit
SCNNodeSetCamera( cameraNode, camera )
SCNNodeAddChildNode( rootNode, cameraNode )
SCNVector3 cameraPos = {0.0, 0.0, 10.0}
SCNNodeSetPosition( cameraNode, cameraPos )
SCNNodeRef lightNode = fn SCNNodeInit
SCNLightRef light = fn SCNLightInit
SCNLightSetType( light, SCNLightTypeOmni )
SCNNodeSetPosition( lightNode, fn SCNVector3Make( 0.0, 10.0, 10.0 ) )
SCNNodeAddChildNode( rootNode, lightNode )
SCNNodeRef ambientLightNode = fn SCNNodeInit
SCNLightRef ambientLight = fn SCNLightInit
SCNLightSetType( ambientLight, SCNLightTypeAmbient )
SCNLightSetColor( ambientLight, fn ColorGray )
SCNNodeSetLight( ambientLightNode, ambientLight )
SCNNodeAddChildNode( rootNode, ambientLightNode )
SCNBoxRef boxGeometry = fn SCNBoxInit( 2.0, 3.0, 4.0, 0.0 )
SCNNodeRef boxNode = fn SCNNodeWithGeometry( boxGeometry )
SCNMaterialRef side1 = fn SCNMaterialInit
SCNMaterialRef side2 = fn SCNMaterialInit
SCNMaterialRef side3 = fn SCNMaterialInit
SCNMaterialRef side4 = fn SCNMaterialInit
SCNMaterialRef side5 = fn SCNMaterialInit
SCNMaterialRef side6 = fn SCNMaterialInit
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side1 ), fn ColorBlue )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side2 ), fn ColorOrange )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side3 ), fn ColorRed )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side4 ), fn ColorGreen )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side5 ), fn ColorYellow )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side6 ), fn ColorCyan )
SCNGeometrySetMaterials( boxGeometry, @[side1,side2,side3,side4,side5,side6] )
SCNNodeAddChildNode( rootNode, boxNode )
SCNActionableRunAction( boxNode, fn SCNActionRotateByAngle( M_PI, fn SCNVector3Make( 1.0, 1.0, 1.0 ), 0.0 ) )
end fn = scene
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 400, 400 )
window _window, @"Rosetta Code 2-3-4 Cuboid", r
scnview _sceneView, fn Cuboid, r
SCNViewSetBackgroundColor( _sceneView, fn ColorBlack )
SCNViewSetAllowsCameraControl( _sceneView, YES )
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select (ev)
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:FutureBasic_cuboid.png]]
 
=={{header|Go}}==
Line 1,826 ⟶ 2,212:
}
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
{{trans|javascript}} An adaptation working in the lambdaway project (where wiki pages are editable in realtime and inline:): http://lambdaway.free.fr/lambdawalks/?view=cuboid .
<syntaxhighlight lang="scheme">
1) creating the canvas
 
{canvas {@ width="580" height="580"}}
 
2) calling javascript
 
{script
var canvas, g, width, height;
var mouseX = 0, prevMouseX, mouseY = 0, prevMouseY;
 
function run() {
canvas = document.querySelector("canvas");
width = canvas.width;
height = canvas.height;
g = canvas.getContext("2d");
canvas.addEventListener("mousemove",
function (event) {
prevMouseX = mouseX;
prevMouseY = mouseY;
mouseX = event.x;
mouseY = event.y;
 
var incrX = (mouseX - prevMouseX) * 0.01;
var incrY = (mouseY - prevMouseY) * 0.01;
 
rotateCuboid(incrX, incrY);
drawCuboid();
});
 
scale(80, 120, 160);
rotateCuboid(Math.PI / 5, Math.PI / 9);
drawCuboid()
 
}
 
var nodes = [[-1, -1, -1], [-1, -1, 1], [-1, 1, -1], [-1, 1, 1],
[1, -1, -1], [1, -1, 1], [1, 1, -1], [1, 1, 1]];
 
var edges = [[0, 1], [1, 3], [3, 2], [2, 0], [4, 5], [5, 7], [7, 6],
[6, 4], [0, 4], [1, 5], [2, 6], [3, 7]];
 
function scale(factor0, factor1, factor2) {
nodes.forEach(function (node) {
node[0] *= factor0;
node[1] *= factor1;
node[2] *= factor2;
});
}
 
function rotateCuboid(angleX, angleY) {
 
var sinX = Math.sin(angleX);
var cosX = Math.cos(angleX);
 
var sinY = Math.sin(angleY);
var cosY = Math.cos(angleY);
 
nodes.forEach(function (node) {
var x = node[0];
var y = node[1];
var z = node[2];
 
node[0] = x * cosX - z * sinX;
node[2] = z * cosX + x * sinX;
 
z = node[2];
 
node[1] = y * cosY - z * sinY;
node[2] = z * cosY + y * sinY;
});
}
 
function drawCuboid() {
g.save();
g.clearRect(0, 0, width, height);
g.translate(width / 2, height / 2);
g.strokeStyle = "#000";
g.beginPath();
 
edges.forEach(function (edge) {
var p1 = nodes[edge[0]];
var p2 = nodes[edge[1]];
g.moveTo(p1[0], p1[1]);
g.lineTo(p2[0], p2[1]);
});
g.closePath();
g.stroke();
 
g.restore();
}
 
setTimeout( run, 1 )
}
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Line 2,861 ⟶ 3,348:
 
=={{header|POV-Ray}}==
<syntaxhighlight lang="pov-ray">camera { perspective location <2.6,2.2,-4.2> look_at <0,-.5,0>
aperture .05 blur_samples 100 variance 1/100000 focal_point <2,1,-2>}
Line 4,193 ⟶ 4,680:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var cubLine = Fn.new { |n, dx, dy, cde|
Line 4,353 ⟶ 4,840:
SetVid(3); \restore normal text mode (for DOS)
]</syntaxhighlight>
 
=={{header|Zig}}==
{{trans|Factor}}
{{libheader|raylib}}
<syntaxhighlight lang="zig">
const std = @import("std");
const c = @cImport({
@cInclude("raylib.h");
});
 
pub fn main() !void {
c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_VSYNC_HINT);
c.InitWindow(600, 480, "cuboid");
defer c.CloseWindow();
 
const camera = c.Camera3D{
.position = .{ .x = 4.5, .y = 4.5, .z = 4.5 },
.target = .{ .x = 0, .y = 0, .z = 0 },
.up = .{ .x = 0, .y = 1, .z = 0 },
.fovy = 45.0,
.projection = c.CAMERA_PERSPECTIVE,
};
 
c.SetTargetFPS(60);
 
while (!c.WindowShouldClose()) {
c.BeginDrawing();
defer c.EndDrawing();
 
c.ClearBackground(c.BLACK);
 
{
c.BeginMode3D(camera);
defer c.EndMode3D();
 
c.DrawCubeWires(.{ .x = 0, .y = 0, .z = 0 }, 2, 3, 4, c.LIME);
}
}
}
</syntaxhighlight>
 
=={{header|zkl}}==
12

edits