Death Star: Difference between revisions

m
→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work
No edit summary
m (→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work)
(11 intermediate revisions by 4 users not shown)
Line 285:
SDL.Finalise;
end Death_Star;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
{{Trans|C}}{{Trans|11l}}
<syntaxhighlight lang="algol68">
BEGIN # draw a "Death Star" - translated from the C and 11l samples #
STRING shades = ".:!*oe&#%@";
 
PROC normalize = ( []REAL v )[]REAL:
BEGIN
REAL len = sqrt( v[ 1 ] * v[ 1 ] + v[ 2 ] * v[ 2 ] + v[ 3 ] * v[ 3 ] );
( v[ 1 ] / len, v[ 2 ] / len, v[ 3 ] / len )
END # normalize # ;
 
PROC dot = ( []REAL x, y )REAL:
BEGIN
REAL d = x[ 1 ] * y[ 1 ] + x[ 2 ] * y[ 2 ] + x[ 3 ] * y[ 3 ];
IF d < 0 THEN - d ELSE 0 FI
END # dot # ;
 
MODE SPHERE = STRUCT( REAL cx, cy, cz, r );
 
# positive shpere and negative sphere #
SPHERE pos = ( 20, 20, 0, 20 ), neg = ( 1, 1, -6, 20 );
 
# check if a ray (x,y, -inf)->(x, y, inf) hits a sphere; if so, return #
# the intersecting z values. z1 is closer to the eye #
PROC hit_sphere = ( SPHERE sph, REAL x in, y in, REF REAL z1, z2 )BOOL:
IF REAL x = x in - cx OF sph;
REAL y = y in - cy OF sph;
REAL zsq := r OF sph * r OF sph - ( x * x + y * y );
zsq < 0
THEN FALSE
ELSE zsq := sqrt( zsq );
z1 := cz OF sph - zsq;
z2 := cz OF sph + zsq;
TRUE
FI # hit_sphere # ;
 
PROC draw_sphere = ( REAL k, ambient, []REAL light )VOID:
FOR i FROM ENTIER ( cy OF pos - r OF pos ) TO ENTIER ( cy OF pos + r OF pos ) + 1 DO
REAL y := i + 0.5;
FOR j FROM ENTIER ( cx OF pos - 2 * r OF pos ) TO ENTIER (cx OF pos + 2 * r OF pos ) + 1 DO
REAL x := ( j - cx OF pos ) / 2.0 + 0.5 + cx OF pos;
REAL zb1 := 0, zb2 := 0, zs1 := 0, zs2 := 0;
INT hit_result
= IF NOT hit_sphere( pos, x, y, zb1, zb2 ) THEN
0 # ray lands in blank space, draw bg #
ELIF NOT hit_sphere( neg, x, y, zs1, zs2 ) THEN
1 # ray hits pos sphere but not neg, draw pos sphere surface #
ELIF zs1 > zb1 THEN
1 # ray hits both, but pos front surface is closer #
ELIF zs2 > zb2 THEN
0 # pos sphere surface is inside neg sphere, show bg #
ELIF zs2 > zb1 THEN
2 # back surface on neg sphere is inside pos sphere, #
# the only place where neg sphere surface will be shown #
ELSE
1 # show the pos sphere #
FI;
IF hit_result = 0 THEN
print( ( " " ) )
ELSE
[]REAL vec =
normalize( IF hit_result = 1
THEN []REAL( x - cx OF pos
, y - cy OF pos
, zb1 - cz OF pos
)
ELSE []REAL( cx OF neg - x
, cy OF neg - y
, cz OF neg - zs2
)
FI
);
REAL b = ( dot( light, vec ) ^ k ) + ambient;
INT intensity := ENTIER ( ( 1 - b ) * ( ( UPB shades - LWB shades ) + 1 ) ) + 1;
IF intensity < LWB shades THEN intensity := LWB shades
ELIF intensity > UPB shades THEN intensity := UPB shades
FI;
print( ( shades[ intensity ] ) )
FI
OD;
print( ( newline ) )
OD # draw_sphere # ;
 
BEGIN
[]REAL light = ( -50, 30, 50 );
draw_sphere( 2, 0.5, normalize( light ) )
END
END
</syntaxhighlight>
{{out}}
Same as 11l
 
=={{header|AutoHotkey}}==
Line 1,329 ⟶ 1,422:
}
</syntaxhighlight>
===Using Java 11===
Alternatively, without using JavaFX, which has been removed from the JavaJDK since version 11.
<syntaxhighlight lang="java">
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import javax.imageio.ImageIO;
 
public final class DeathStar {
 
public static void main(String[] aArgs) throws IOException {
Vector direction = new Vector(20.0, -40.0, -10.0);
direction.normalise();
Sphere positive = new Sphere(0, 0, 0, 120);
Sphere negative = new Sphere(-90, -90, -30, 100);
 
BufferedImage image = deathStar(positive, negative, direction, 1.5, 0.5);
ImageIO.write(image, "png", new File("DeathStarJava.png"));
}
private static BufferedImage deathStar(
Sphere aPositive, Sphere aNegative, Vector aDirection, double aShadow, double aBrightness) {
final int width = aPositive.radius * 4;
final int height = aPositive.radius * 3;
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = result.getGraphics();
graphics.setColor(Color.CYAN);
graphics.fillRect(0, 0, width, height);
Vector ray = new Vector(0.0, 0.0, 0.0);
final int deltaX = aPositive.x - width / 2;
final int deltaY = aPositive.y - height / 2;
 
double xMax = aPositive.x + aPositive.radius;
double yMax = aPositive.y + aPositive.radius;
for ( int y = aPositive.y - aPositive.radius; y < yMax; y++ ) {
for ( int x = aPositive.x - aPositive.radius; x < xMax; x++ ) {
List<Object> contacts = aPositive.contact(x, y);
final double zb1 = (double) contacts.get(0);
final int zb2 = (int) contacts.get(1);
final boolean positiveHit = (boolean) contacts.get(2);
if ( ! positiveHit ) {
continue;
}
contacts = aNegative.contact(x, y);
final double zs1 = (double) contacts.get(0);
final int zs2 = (int) contacts.get(1);
boolean negativeHit = (boolean) contacts.get(2);
if ( negativeHit ) {
if ( zs1 > zb1 ) {
negativeHit = false;
} else if ( zs2 > zb2 ) {
continue;
}
}
if ( negativeHit ) {
ray.x = aNegative.x - x;
ray.y = aNegative.y - y;
ray.z = aNegative.z - zs2;
} else {
ray.x = x - aPositive.x;
ray.y = y - aPositive.y;
ray.z = zb1 - aPositive.z;
}
ray.normalise();
double rayComponent = ray.scalarProduct(aDirection);
if ( rayComponent < 0 ) {
rayComponent = 0;
}
int color = (int) ( 255 * ( Math.pow(rayComponent, aShadow) + aBrightness) / ( 1 + aBrightness ) );
if ( color < 0 ) {
color = 0;
} else if ( color > 255 ) {
color = 255;
}
result.setRGB(x - deltaX, y - deltaY, color);
}
}
return result;
}
private static class Vector {
public Vector(double aX, double aY, double aZ) {
x = aX; y = aY; z = aZ;
}
public double scalarProduct(Vector aOther) {
return x * aOther.x + y * aOther.y + z * aOther.z;
}
public Vector normalise() {
final double magnitude = Math.sqrt(this.scalarProduct(this));
return new Vector(x /= magnitude, y /= magnitude, z /= magnitude);
}
private double x, y, z;
}
private static class Sphere {
public Sphere(int aX, int aY, int aZ, int aRadius) {
x = aX; y = aY; z = aZ; radius = aRadius;
}
public List<Object> contact(int aX, int aY) {
final int xx = aX - x;
final int yy = aY - y;
final int zSquared = radius * radius - ( xx * xx + yy * yy );
if ( zSquared >= 0 ) {
final double zz = Math.sqrt(zSquared);
return List.of(z - zz, z, true);
}
return List.of( 0.0, 0, false );
}
private int x, y, z, radius;
}
 
}
</syntaxhighlight>
{{ out }}
[[Media:DeathStarJava.png]]
 
=={{header|JavaScript}}==
Line 1,843 ⟶ 2,068:
 
=={{header|POV-Ray}}==
<syntaxhighlight lang="pov-ray">camera { perspective location <0.0 , .8 ,-3.0> look_at 0
aperture .1 blur_samples 20 variance 1/100000 focal_point 0}
Line 2,686 ⟶ 2,911:
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, ImageData
import "math" for Vector
Line 2,951 ⟶ 3,176:
{{trans|C}}
Primitive ray tracing. Writes a PGM to stdout.
<syntaxhighlight lang="zig">const std = @import("std");
const std = @import("std");
const Allocator = std.mem.Allocator;
</syntaxhighlight>
 
<syntaxhighlight lang="zig">pub fn main() !void {
// buffer stdout --------------------------------------
const stdout_file = std.io.getStdOut().writer();
Line 2,982 ⟶ 3,208:
// ----------------------------------------------------
try bw.flush();
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">fn Vector(comptime T: type) type {
 
fn Vector(comptime T: type) type {
return struct {
const Self = @This();
Line 3,010 ⟶ 3,235:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn SphereHit(comptime T: type) type {
return struct { z1: T, z2: T };
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn Sphere(comptime T: type) type {
return struct {
Line 3,032 ⟶ 3,257:
const x = xx - self.cx;
const y = yy - self.cy;
varconst zsq = self.r * self.r - x * x - y * y;
if (zsq >= 0) {
varconst zsqrt = std.math.sqrt(zsq);
return .{ .z1 = self.cz - zsqrt, .z2 = self.cz + zsqrt };
}
Line 3,040 ⟶ 3,265:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn DeathStar(comptime T: type) type {
return struct {
Line 3,062 ⟶ 3,287:
const amb: T = 0.2;
 
varconst w: usize = @floatToIntintFromFloat(usize, pos.r * 4);
varconst h: usize = @floatToIntintFromFloat(usize, pos.r * 3);
var img = try ImageData().init(allocator, "deathStar", w, h);
 
var vec = Vector(T).zero();
 
const start_y: usize = @floatToIntintFromFloat(usize, pos.cy - pos.r);
const end_y: usize = @floatToIntintFromFloat(usize, pos.cy + pos.r);
const start_x: usize = @floatToIntintFromFloat(usize, pos.cx - pos.r);
const end_x: usize = @floatToIntintFromFloat(usize, pos.cx + pos.r);
 
for (start_y..end_y + 1) |j| {
for (start_x..end_x + 1) |i| {
const x: T = @intToFloatfloatFromInt(T, i);
const y: T = @intToFloatfloatFromInt(T, j);
 
const result_pos = pos.hit(x, y);
Line 3,105 ⟶ 3,330:
if (s < 0) s = 0;
const lum = 255 * (std.math.pow(T, s, k) + amb) / (1 + amb);
const lumi: u8 = @floatToIntintFromFloat(u8, std.math.clamp(lum, 0, 255));
img.pset(i, j, Gray{ .w = lumi });
}
Line 3,142 ⟶ 3,367:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
const Gray = struct {
w: u8,
const black = Gray{ .w = 0 };
};</syntaxhighlight>
};
<syntaxhighlight lang="zig">
 
fn ImageData() type {
return struct {
Line 3,172 ⟶ 3,397:
/// Write PGM P2 ASCII to 'writer'
pub fn print(self: *const Self, writer: anytype, optional_comments: ?[]const []const u8) !void {
_ = try writer.print("P2\n", .{});
 
if (optional_comments) |lines| {
for (lines) |line|
_ = try writer.print("# {s}\n", .{line});
}
 
_ = try writer.print("{d} {d}\n{d}\n", .{ self.w, self.h, 255 });
 
for (self.image, 0..) |pixel, i| {
const sep = if (i % self.w == self.w - 1) "\n" else " ";
_ = try writer.print("{d}{s}", .{ pixel.w, sep });
}
}
12

edits