0: =pod
1: Well, I was bored the other day and started playing around with one of the silly pastimes that I
2: amuse myself with (ASCII art). Then I thought, "Hey, I can incorporate another one of the things
3: I amuse myself with!" That was Perl, and so I started fiddling aroud with Perl scripts to make
4: ASCII Art. Then I though, "Hey, I can incorporate another one of the things I amuse myself with!"
5: That was n-dimensional geometry, and that was also 4 days ago. This is the net-result.
6:
7: Just run this little puppy, and it will happily print out objects up to 6 dimensions. It can
8: only draw cubes, and at the moment it can't go higher than 6 dimensions (since there aren't many
9: characters after that that look good unless you want to duplicate things)
10:
11: I'm sure this could be written more prettily, compactly, extensibly, and so on. It's fairly dumb
12: at the moment. But gosh-darn it, it does do this one thing pretty well.
13:
14: And I just *know* that you've been dying to see an ASCII hyper cube, so take a look at it. :)
15:
16: The thing that I'm most pleased with, is that they're actually drawn by their mathematical definitions.
17: So a point is zero dimensional, a line is a moving point, a plane a moving line, and so on. It
18: places the objects and then connects them to form the next higher dimension. No lame-o caching
19: of previously created ASCII for me! No sir.
20:
21: If, for some arcane reason, someone wants a more robust version of this thing, I may look into it. But,
22: realistically, such endeavors are best left as an exercise for the reader. ;)
23:
24: More intelligent methods to add are the ability to have more complex objects, the difference between
25: 'front' and 'back' and a couple of other things. I'll email ideas I have about it, if you'd like, but
26: otherwise the weekend is over and I'm done with this little diversion.
27:
28: =cut
29:
30: use strict;
31:
32: #define the ascii characters for each direction
33: my @hor = qw(- =);
34: my @ver = qw(| !);
35: my @ud = qw(/);
36: my @dd = qw(\\);
37:
38: #create our universe
39: make_space();
40:
41: #well, a zero dimensional object is a point
42: my $point = "+";
43:
44: #take a point,
45: place_in_space($point, 0, 0);
46: #move it somewhere else,
47: place_in_space($point, 7,0);
48: #and draw a line between them, and you have:
49: make_path();
50:
51: #a one dimensional object (a line)
52: my $line = space_rip();
53:
54: #take a line,
55: place_in_space($line, 4, 5);
56: #move it somewhere else,
57: place_in_space($line, 4, 10);
58: #and draw a line between them, and you have:
59: make_path();
60:
61: #a two dimensional object (a square)
62: my $square = space_rip();
63:
64: #take a square,
65: place_in_space($square, 4, 5);
66: #move it somwhere else,
67: place_in_space($square, 7, 8);
68: #and draw a line between them, and you have:
69: make_path();
70:
71: #a three dimensional object (a cube)
72: my $cube = space_rip();
73:
74: #take a cube,
75: place_in_space ($cube,5,12);
76: #move it somewhere else,
77: place_in_space ($cube,15,2);
78: #and draw a line between them, and you have:
79: make_path();
80:
81: #a four dimensional object (a hyper cube)
82: my $hypercube = space_rip();
83:
84: #take a hypercube,
85: place_in_space($hypercube, 5, 12);
86: #move it somewhere else,
87: place_in_space($hypercube, 30, 12);
88: #and draw a line between them, and you have:
89: make_path();
90:
91: #a five dimensional object (hyper hyper cube)
92: my $cube5d = space_rip();
93:
94: #take a 5d cube,
95: place_in_space($cube5d, 5, 12);
96: #move it somewhere else,
97: place_in_space($cube5d, 5, 34);
98: #and draw a line between them, and you have:
99: make_path();
100:
101: #a six dimensional object (hyper hyper hyper cube)
102: my $cube6d = space_rip();
103:
104: #and we'll just stop at 6 dimensions, what with
105: #the lack of good ascii characters and all...
106:
107: #let's see what we made!
108:
109: print "=====\nPoint:\n=====\n\n$point\n\n=====\n\n\n";
110: print "=====\nLine:\n=====\n$line\n\n=====\n\n\n";
111: print "=====\nSquare:\n=====\n$square\n\n=====\n\n\n";
112: print "=====\nCube:\n=====\n$cube\n\n=====\n\n\n";
113: print "=====\nHyper Cube:\n=====\n$hypercube\n\n=====\n\n\n";
114: print "=====\n5d Cube:\n=====\n$cube5d\n\n=====\n\n\n";
115: print "=====\n6d Cube:\n=====\n$cube6d\n\n=====";
116:
117: BEGIN {
118:
119: my @space = ();
120:
121: sub make_space () {
122: foreach my $x (0..100){
123: foreach my $y (0..100){
124: $space[$x]->[$y] = " ";
125: };
126: };
127: };
128:
129: my %space = ();
130: my $which = 0;
131: my $node = 0;
132: my $min_x = 10000;
133:
134: sub place_in_space {
135: my ($cube, $x, $y) = @_;
136:
137: $min_x = $x if $x < $min_x;
138:
139: my @cube = map {[split // ]} split(/\n/,$cube);
140:
141: my $node = 0;
142:
143: foreach (@cube){
144: foreach my $z (0..$#$_){
145: next if $_->[$z] =~ /^\s*$/;
146: my $i = $x + $z;
147: $space[$y]->[$i] = $_->[$z];
148: $space{"space" . $which . "p" . $node++}
149: = [$y, $i] if $_->[$z] eq "+";
150: };
151: $y++;
152: };
153:
154: $which++;
155:
156: };
157:
158: sub space_rip() {
159: my $rip = undef;
160: foreach (@space){
161: my $dim = join ("", @$_);
162: next if $dim =~ /^\s*$/;
163: $dim =~ s/(^\s{$min_x}|\s+$)//g;
164: $rip .= "\n$dim";
165: };
166: make_space();
167: $min_x = 100000;
168: return $rip;
169: };
170:
171: sub make_path() {
172: my $htoken = shift @hor;
173: my $vtoken = shift @ver;
174: my $utoken = shift @ud;
175: my $dtoken = shift @dd;
176: my ($use_h, $use_v, $use_ud, $use_dd) = (0,0,0,0);
177: foreach my $cube (0..$which){
178: my $node = 0;
179:
180: while (defined (my $alpha_node =
181: $space{"space" . $cube . "p" . $node})){
182: my $cube2 = $cube + 1;
183: my $beta_node =
184: $space{"space" . $cube2 . "p" . $node} or last;
185:
186: my ($ax, $ay) = @$alpha_node;
187: my ($bx, $by) = @$beta_node;
188:
189:
190: my $max_y = max($ay, $by);
191: my $min_y = min($ay, $by);
192:
193: my $max_x = max($ax, $bx);
194: my $min_x = min($ax, $bx);
195:
196: if ($ax == $bx){ #horizontal
197: $use_h++;
198: foreach (1..($max_y - $min_y - 1)){
199: $space[$ax]->[$min_y + $_] = $htoken
200: unless $space[$ax]->[$min_y + $_]
201: =~ /[$htoken@hor+]/o;
202: };
203: }
204: elsif ($ay == $by){ #vertical
205: $use_v++;
206: foreach (1..($max_x - $min_x - 1)){
207: $space[$min_x + $_]->[$ay] = $vtoken
208: unless $space[$min_x + $_]->[$ay]
209: =~ /[$vtoken@ver+]/o;
210: };
211: }
212: elsif ($by > $ay && $bx > $ax) { #down diagonal
213: return undef unless delta($ax, $bx) ==
214: delta($ay, $by);
215: $use_dd++;
216: foreach (1..($max_x - $min_x - 1)){
217: $space[$min_x + $_]->[$min_y + $_] = $dtoken
218: unless $space[$min_x + $_]->[$min_y + $_]
219: =~ /[$dtoken@dd+]/o;
220: };
221: }
222: else { #up diagonal
223: return undef unless delta($ax, $bx) ==
224: delta($ay, $by);
225: $use_ud++;
226: foreach (1..($max_x - $min_x - 1)){
227: $space[$min_x + $_]->[$max_y - $_] = $utoken
228: unless $space[$min_x + $_]->[$max_y - $_]
229: =~ /[$utoken@ud+]/o;
230: };
231: };
232: delete $space{"space" . $cube2 . "p" . $node};
233: delete $space{"space" . $cube . "p" . $node};
234: $node++;
235: };
236: };
237:
238: unshift @hor, $htoken unless $use_h;
239: unshift @ver, $vtoken unless $use_v;
240: unshift @dd, $dtoken unless $use_dd;
241: unshift @ud, $utoken unless $use_ud;
242: };
243:
244: sub max {
245: my ($a, $b) = @_;
246: return $a > $b ? $a : $b;
247: };
248:
249:
250: sub min {
251: my ($a, $b) = @_;
252: return $a > $b ? $b : $a;
253: };
254:
255:
256: sub delta {
257: my ($a, $b) = @_;
258: return abs($a - $b);
259: };
260: };
261:
In reply to perl in multiple dimensions by jimt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |