Rotating ascii cube (Pascal) - PseudoMander - Jun 3, 2012
Code
program ascii3d;
uses crt;
type
TPoint = array[0..1] of Real;
Function XOffset(Z:Real) : Real;
Begin
XOffset := Z/2;
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Function YOffset(Z:Real) : Real;
Begin
YOffset := Z/3;
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Function Transform(X:Real;Y:Real;Z:Real) :TPoint;
VAR
P : Array[0..1] Of Real;
Begin
P[0]:=X+XOffset(Z)+20;
P[1]:=Y+YOffset(Z)+20;
Transform:= P;
End;
var
count: integer;
canvas : Array[1..40,1..40] of char;
i:tpoint;
i2:tpoint;
temp:integer;
steps:integer;
ti:integer;
temp2:integer;
chrr:char;
x:real;
y:real;
z:real;
begin
count:=0;
while 'a'='a' do begin
count:= (count+2)mod 360;
ClrScr;
Writeln('');
for temp:= 1 to 40 do begin
for temp2:= 1 to 40 do begin
canvas[temp][temp2]:='.';
end;end;
for temp := 0 to 3 do begin
for temp2 := 0 to 1 do begin
x:=Cos((temp /4.0 +count/360)* pi*2)*16.6;
y:=(temp2-0.5)*2*7;
z:=Sin((temp /4.0 +count/360)* pi*2)*16.6;
i:=Transform(x,y,z);
i2:=Transform(x,-y,z);
Steps:=16;
for ti := 1 to Steps do begin
canvas[round(i2[1]-((i2[1]-i[1])/ti))][round(i2[0]-((i2[0]-i[0])/ti))]:='&';
end;
x:=Cos(((temp+1) /4.0 +count/360)* pi*2)*16.6;
y:=(temp2-0.5)*2*7;
z:=Sin(((temp+1) /4.0 +count/360)* pi*2)*16.6;
i2:=Transform(x,y,z);
Steps:=16;
for ti := 1 to Steps do begin
canvas[round(i2[1]-((i2[1]-i[1])/ti))][round(i2[0]-((i2[0]-i[0])/ti))]:='&';
end;
Canvas[round(i[1])][round(i[0])]:='&';
end;
end;
for temp:= 1 to 40 do begin
for temp2:= 1 to 40 do begin
chrr:= canvas[temp][temp2];
if chrr='.' then TextColor(LightBlue) ;
if chrr=',' then TextColor(Red) ;
if chrr='&' then TextColor(White) ;
Write(chrr);
end;
Writeln('');
end;
Delay(60);
end;
end.
Cool stuff.
Nice example of what can bit of math achieve.
* - PseudoMander - Jun 3, 2012
(Jun 3, 2012 07:28 AM)Quentin Wrote: That's pretty amazing, what made you do this? Was it for a class?
I wrote one in C a while back, I usually try to work with a multidimensional array, integers, floats, chars and functions as early as possible when familiarising myself with a language, the ascii canvas seems a sure fire solution to learning the basics of a new imperative language and thus variants of such are my equivalent of "hello world" when I need to learn or revise syntax.
program ascii3d;
uses crt;
type
TPoint = array[0..1] of Real;
Function XOffset(Z:Real) : Real;
Begin
XOffset := Z/2;
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Function YOffset(Z:Real) : Real;
Begin
YOffset := Z/3;
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Function Transform(X:Real;Y:Real;Z:Real) :TPoint;
VAR
P : Array[0..1] Of Real;
Begin
P[0]:=X+XOffset(Z)+70;
P[1]:=Y+YOffset(Z)+30;
Transform:= P;
End;
var
count: integer;
canvas : Array[1..60,1..140] of char;
i:tpoint;
i2:tpoint;
temp:integer;
steps:integer;
ti:integer;
temp2:integer;
chrr:char;
x:real;
y:real;
z:real;
begin
count:=0;
while 'a'='a' do begin
count:= (count+1)mod 360;
ClrScr;
Writeln('');
for temp:= 1 to 60 do begin
for temp2:= 1 to 140 do begin
canvas[temp][temp2]:='.';
end;end;
for temp := 0 to 7 do begin
for temp2 := 0 to 1 do begin
x:=Cos((temp /8.0 +count/360)* pi*2)*34.6;
y:=(temp2-0.5)*2*13;
z:=Sin((temp /8.0 +count/360)* pi*2)*34.6;
i:=Transform(x,y,z);
i2:=Transform(Cos((temp /32.0 +count/360)* pi*2)*1,-y,Cos((temp /32.0 +count/360)* pi*2)*1);
Steps:=32;
for ti := 1 to Steps do begin
canvas[round(i2[1]-((i2[1]-i[1])/ti))][round(i2[0]-((i2[0]-i[0])/ti))]:='&';
end;
x:=Cos(((temp+1) /8.0 +count/360)* pi*2)*34.6;
y:=(temp2-0.5)*2*13;
z:=Sin(((temp+1) /8.0 +count/360)* pi*2)*34.6;
i2:=Transform(x,y,z);
Steps:=32;
for ti := 1 to Steps do begin
canvas[round(i2[1]-((i2[1]-i[1])/ti))][round(i2[0]-((i2[0]-i[0])/ti))]:='&';
end;
Canvas[round(i[1])][round(i[0])]:='&';
end;
end;
for temp:= 1 to 60 do begin
for temp2:= 1 to 140 do begin
chrr:= canvas[temp][temp2];
if chrr='.' then TextColor(LightBlue) ;
if chrr=',' then TextColor(Red) ;
if chrr='&' then TextColor(White) ;
Write(chrr);
end;
Writeln('');
end;
Delay(60);
end;
end.
I'll put it in a spoiler tag to save room since it's astoundingly similar to the previous shape.