Death Star: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 27:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Sphere
Float cx, cy, cz, r
F (cx, cy, cz, r)
Line 93:
 
V light = normalize((-50.0, 30.0, 50.0))
draw_sphere(2, 0.5, light)</langsyntaxhighlight>
 
{{out}}
Line 142:
{{libheader|SDLAda}}{{trans|Go}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Generic_Real_Arrays;
 
Line 284:
Window.Finalize;
SDL.Finalise;
end Death_Star;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<langsyntaxhighlight lang="ahk">#NoEnv
SetBatchLines, -1
#SingleInstance, Force
Line 367:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|Brlcad}}==
<langsyntaxhighlight lang="brlcad"># We need a database to hold the objects
opendb deathstar.g y
 
Line 395:
 
# We now trigger the raytracer to see our finished product
rt</langsyntaxhighlight>
 
=={{header|C}}==
Primitive ray tracing.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <unistd.h>
Line 510:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.numeric, std.algorithm;
 
struct V3 {
Line 622:
immutable light = [-50, 30, 50].V3.normalize;
drawSphere(2, 0.5, light);
}</langsyntaxhighlight>
 
The output is the same of the C version.
Line 633:
{{Trans|C}}
Translate of [[#C]] and [[#Go]], with copy of some parts of [[#DWScript]].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Death_Star;
 
Line 787:
Free;
end;
end.</langsyntaxhighlight>
=={{header|DWScript}}==
{{trans|C}}
<langsyntaxhighlight lang="delphi">const cShades = '.:!*oe&#%@';
 
type TVector = array [0..2] of Float;
Line 883:
Normalize(light);
DrawSphere(2, 0.3);</langsyntaxhighlight>
 
=={{header|Frink}}==
This program not only draws a Death Star and renders it onscreen projected on the x,y, and z axes but also outputs a .stl file for 3-D printing. Frink has [https://frinklang.org/3d/frink/graphics/package-summary.html built-in routines for 3-D modeling].
<langsyntaxhighlight lang="frink">res = 254 / in
v = callJava["frink.graphics.VoxelArray", "makeSphere", [1/2 inch res]]
 
Line 904:
w.close[]
println["done."]
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
[[file:GoDstar.png|right|thumb|Output png]]
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,011:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 1,017:
=== ASCII art ===
 
<langsyntaxhighlight lang="haskell">import Data.List (genericLength)
 
shades = ".:!*oe%#&@"
Line 1,039:
| otherwise = [x, y, sph1 x y]
sph1 x y = sqrt (r*r - x*x - y*y)
sph2 x y = r - sqrt (r*r - (x+r)**2 - (y+r)**2)</langsyntaxhighlight>
 
<pre>λ> putStrLn $ deathStar 10 4 0.1
Line 1,070:
{{Trans|Python}}
 
<syntaxhighlight lang="j">
<lang J>
load'graphics/viewmat'
mag =: +/&.:*:"1
Line 1,110:
env=.(2; 0.5; (norm _50 30 50))
sph=. 20 20 0; 20; 1 1 _6; 20
'rgb' viewmat togray env draw_sphere sph</langsyntaxhighlight>
 
=={{header|Java}}==
{{libheader|JavaFX}}
<langsyntaxhighlight Javalang="java">import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
Line 1,328:
 
}
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
Layer circles and gradients to achieve result similar to that of the Wikipedia page for the [http://en.wikipedia.org/wiki/Death_Star Death Star].
<syntaxhighlight lang="javascript">
<lang JavaScript>
<!DOCTYPE html>
<html>
Line 1,375:
</html>
 
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># run in REPL
using GLMakie
 
Line 1,396:
 
scene = deathstar()
</syntaxhighlight>
</lang>
 
=={{header|LSL}}==
Rez a box on the ground, raise it up a few meters, add the following as a New Script.
<langsyntaxhighlight LSLlang="lsl">default {
state_entry() {
llSetPrimitiveParams([PRIM_NAME, "RosettaCode DeathStar"]);
Line 1,413:
llSetPrimitiveParams([PRIM_OMEGA, <0.0, 0.0, 1.0>, 1.0, 1.0]);
}
}</langsyntaxhighlight>
Output:
[[File:Death_Star_LSL.jpg|200px|Death Star]]
Line 1,419:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function V3(x,y,z) return {x=x,y=y,z=z} end
function dot(v,w) return v.x*w.x + v.y*w.y + v.z*w.z end
function norm(v) local m=math.sqrt(dot(v,v)) return V3(v.x/m, v.y/m, v.z/m) end
Line 1,460:
end
 
deathstar({x=20, y=20, z=0, r=20}, {x=10, y=10, z=-15, r=10}, norm(V3(-2,1,3)), 2, 0.1)</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%"> @@@%%%%%%%%%#########%
Line 1,504:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">with(plots):
with(plottools):
plots:-display(
implicitplot3d(x^2 + y^2 + z^2 = 1, x = -1..0.85, y = -1..1, z = -1..1, style = surface, grid = [50,50,50]),
translate(rotate(implicitplot3d(x^2 + y^2 + z^2 = 1, x = 0.85..1, y = -1..1, z = -1..1, style = surface, grid = [50,50,50]), 0, Pi, 0), 1.70, 0, 0),
axes = none, scaling = constrained, color = gray)</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">RegionPlot3D[x^2 + y^2 + z^2 < 1 && (x + 1.7)^2 + y^2 + z^2 > 1,
{x, -1, 1}, {y, -1, 1}, {z, -1, 1},
Boxed -> False, Mesh -> False, Axes -> False, Background -> Black, PlotPoints -> 100]</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,522:
The result is written in a PNG file. For this purpose, we used the modules “bitmap” and “grayscale_image” created for the tasks “Bitmap” and “Grayscale image”. To write the PNG file, we use the third party library “nimPNG”.
 
<langsyntaxhighlight Nimlang="nim">import math
 
import bitmap, grayscale_image, nimPNG
Line 1,611:
for color in rgbImage.pixels:
data.add([color.r, color.g, color.b])
echo savePNG24("death_star.png", data, rgbImage.w, rgbImage.h)</langsyntaxhighlight>
 
=={{header|Openscad}}==
<langsyntaxhighlight lang="openscad">// We are performing geometric subtraction
 
difference() {
Line 1,632:
sphere(40);
}
}</langsyntaxhighlight>
 
=={{header|Perl}}==
[[file:death-star-perl.png|thumb]]
Writes a PGM to stdout.
<langsyntaxhighlight lang="perl">use strict;
 
sub sq {
Line 1,711:
}
 
draw(2, 0.2);</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 1,718:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/deathstar.htm here]. Note it is rather slow to redraw fullscreen.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\DeathStar.exw
Line 1,840:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|POV-Ray}}==
<langsyntaxhighlight POVlang="pov-Rayray">camera { perspective location <0.0 , .8 ,-3.0> look_at 0
aperture .1 blur_samples 20 variance 1/100000 focal_point 0}
Line 1,859:
finish { phong 1 reflection {0.10 metallic 0.5} }
}
} </langsyntaxhighlight>
[[image:PovRay-deathstar.jpg]]
 
=={{header|Python}}==
{{trans|C}}
<langsyntaxhighlight lang="python">import sys, math, collections
 
Sphere = collections.namedtuple("Sphere", "cx cy cz r")
Line 1,930:
 
light = normalize(V3(-50, 30, 50))
draw_sphere(2, 0.5, light)</langsyntaxhighlight>
 
=={{header|Q}}==
write an image in BMP format:
<syntaxhighlight lang="q">
<lang Q>
/ https://en.wikipedia.org/wiki/BMP_file_format
/ BITMAPINFOHEADER / RGB24
Line 1,977:
/ fn:`:demo.bmp;
/ writebmp[w;h;fcn;fn];
</syntaxhighlight>
</lang>
 
Create the death star image:
 
<syntaxhighlight lang="q">
<lang Q>
w:400; h:300; r:150; l:-0.5 0.7 0.5
sqrt0:{$[x>0;sqrt x;0]};
Line 2,021:
writebmp[w;h;fcn;fn];
 
</langsyntaxhighlight>(converted to JPG ...)
 
[[image:qdstar.jpg]]
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require plot)
Line 2,036:
#:y-min -1/2 #:y-max 1/2
#:z-min 0 #:z-max 1)
</syntaxhighlight>
</lang>
[[File:death-star.png]]
 
Line 2,044:
{{works with|Rakudo|2018.10}}
[[File:Deathstar-perl6.png|thumb]]
<syntaxhighlight lang="raku" perl6line>class sphere {
has $.cx; # center x coordinate
has $.cy; # center y coordinate
Line 2,130:
$z = $sphere.cz - $z2 .. $sphere.cz + $z2;
True;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 2,136:
 
(Apologies for the comments making the lines so wide, but it was easier to read and compare to the original &nbsp; '''D''' &nbsp; source.)
<langsyntaxhighlight lang="rexx">/*REXX program displays a sphere with another sphere subtracted where it's superimposed.*/
call deathStar 2, .5, v3('-50 30 50')
exit /*stick a fork in it, we're all done. */
Line 2,189:
if @\='' then say strip(@, 'T') /*strip trailing blanks from line. */
end /*i*/ /* [↑] show all lines for sphere. */
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
(Shown at &nbsp; <big>'''<sup>1</sup>/<sub>2</sub>'''</big> &nbsp; size.)
Line 2,236:
 
=={{header|Set lang}}==
<langsyntaxhighlight lang="set_lang">set ! 32
set ! 32
set ! 46
Line 2,295:
set ! 46
set ! 45
set ! 126</langsyntaxhighlight>
Outputs:
<pre> .-~""~-.
Line 2,307:
{{trans|Perl}}
Writes a PGM to stdout.
<langsyntaxhighlight lang="ruby">func hitf(sph, x, y) {
x -= sph[0]
y -= sph[1]
Line 2,368:
}
 
draw(2, 0.2)</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/death_star_sidef.png here].
 
Line 2,374:
{{trans|C}}
Note that this code has a significant amount of refactoring relative to the C version, including the addition of specular reflections and the separation of the scene code from the raytracing from the rendering.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc normalize vec {
Line 2,488:
}
}
textDeathStar 3 10 0.7 0.3</langsyntaxhighlight>
Output:
<pre>
Line 2,535:
{{libheader|Tk}}
[[File:Deathstar-tcl.gif|200px|thumb|Rendering of the Death Star by the Tcl solution.]]
<langsyntaxhighlight lang="tcl"># Render as a picture (with many hard-coded settings)
package require Tk
proc guiDeathStar {photo diff spec lightBrightness ambient} {
Line 2,552:
}
pack [label .l -image [image create photo ds]]
guiDeathStar ds 3 10 0.7 0.3</langsyntaxhighlight>
 
=={{header|VBScript}}==
ASCII graphics. Should be invoked with cscript. Modified from LUA
<syntaxhighlight lang="vb">
<lang vb>
'deathstar ascii graphics
 
Line 2,638:
 
deathstar array(20, 20, 0, 20),array(10,10,-15,10), normal(array(-2,1,3)), 2, 0.1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,686:
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color, ImageData
import "math" for Vector
Line 2,789:
}
 
var Game = DeathStar.new(400, 400)</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 100,100
window origin "cc"
backcolor 0,0,0
Line 2,947:
draw_sphere(2, .3)
wend
</syntaxhighlight>
</lang>
10,327

edits