Draw a sphere: Difference between revisions

→‎{{header|Locomotive Basic}}: add graphical version
(→‎{{header|Locomotive Basic}}: add graphical version)
 
(13 intermediate revisions by 7 users not shown)
Line 991:
3020 RETURN
</syntaxhighlight>
 
[[File:Cpcbasic sphere.png|thumb|Output in CPCBasic]]
The program above can also be adapted to produce graphical output in MODE 2 instead:
 
<syntaxhighlight lang="locobasic">10 MODE 2:ORIGIN 320,200:INK 0,0:INK 1,26
20 DIM v(2),vec(2)
30 v(0)=30:v(1)=30:v(2)=-50
40 lung=SQR(v(0)*v(0)+v(1)*v(1)+v(2)*v(2))
50 v(0)=v(0)/lung
60 v(1)=v(1)/lung
70 v(2)=v(2)/lung
80 r=180:k=1.5:ambient=0.03
90 FOR i=INT(-r) TO INT(r)
100 x=i
110 FOR j=INT(-2*r) TO INT(2*r)
120 y=j/2
130 IF (x*x+y*y<=r*r) THEN GOSUB 1000
140 NEXT j
160 NEXT i
170 END
1000 vec(0)=x
1010 vec(1)=y
1020 vec(2)=SQR(r*r-x*x-y*y)
1030 GOSUB 2000
1040 GOSUB 3000
1050 b=(d^k+ambient)/(1+ambient)
1060 IF b>rnd THEN PLOT x,-y
1100 RETURN
2000 lung=SQR(vec(0)*vec(0)+vec(1)*vec(1)+vec(2)*vec(2))
2010 vec(0)=vec(0)/lung
2020 vec(1)=vec(1)/lung
2030 vec(2)=vec(2)/lung
2040 RETURN
3000 d=v(0)*vec(0)+v(1)*vec(1)+v(2)*vec(2)
3010 IF d<0 THEN d=-d ELSE d=0
3020 RETURN</syntaxhighlight>
 
==={{header|PureBasic}}===
Line 1,318 ⟶ 1,354:
rem more info: https://www.dostips.com/forum/viewtopic.php?f=3&t=6744
 
rem integer sqrt arithmetic function by Aacini, penpen and penpeneinstein1969
rem source: https://www.dostips.com/forum/viewtopic.php?f=3&t=5819&start=30#p44016
set "sqrt(N)=( M=(N),j=M/(11*1024)+40, j=(M/j+j)>>1, j=(M/j+j)>>1, j=(M/j+j)>>1, j=(M/j+j)>>1, j=(M/j+j)>>1, j+=(M-j*j)>>31 )"
Line 1,379 ⟶ 1,415:
set /a "sqrdR=R*R*100*100" %== R*R is mult. by 100*100 ==%
set "k=2" %== k is hardcoded to 2 ==%
set "ambient=%32"
rem start draw line-by-line
for /l %%i in (%negR%, 1, %R%) do (
Line 1,993 ⟶ 2,029:
}
</syntaxhighlight>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">let j = 2
 
for i = 221 to 0 step j * -1
 
for k = -3.14 to 3.14 step .01
 
dot 221 + i * sin(k), 222 + 221 * cos(k)
dot 221 + 221 * sin(k), 222 + (i - 1) * cos(k)
 
wait
 
next k
 
let j = j + 1
 
next i</syntaxhighlight>
 
=={{header|D}}==
Line 2,242 ⟶ 2,296:
 
=={{header|Emacs Lisp}}==
[[File:Draw a sphere emacs lisp.png|thumb|Output]]
{{trans|Go}}
<syntaxhighlight lang="lisp">; Draw a sphere
Line 2,420 ⟶ 2,475:
</pre>
 
=={{header|Evaldraw}}==
 
Draw a sphere. We dont need to wrap the main () function with braces if we have no functions.
 
[[File:Evaldraw draw sphere.png|thumb|alt=A red sphere on a black background.|A red sphere, 5 units in front of camera.]]
 
<syntaxhighlight lang="C">
()
cls(0);
clz(1e32);
setcam(0,0,-5,0,0);
setcol(128,64,64);
drawsph(0,0,0,1);
</syntaxhighlight>
 
=={{header|Factor}}==
Line 5,776 ⟶ 5,846:
eeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeee</pre>
 
=={{header|V (Vlang)}}==
Graphical
<syntaxhighlight lang="Zig">
import gg
import gx
 
const (
win_width = 600
win_height = 600
)
 
struct App {
mut:
gg &gg.Context = unsafe {nil}
}
 
fn main() {
mut app := &App{
gg: 0
}
app.gg = gg.new_context(
bg_color: gx.white
width: win_width
height: win_height
create_window: true
window_title: 'Circle'
frame_fn: frame
user_data: app
)
app.gg.run()
}
 
fn frame(app &App) {
app.gg.begin()
app.draw()
app.gg.end()
}
 
fn (app &App) draw() {
app.gg.draw_circle_filled(300, 300, 150, gx.red)
}
</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|C}}
<syntaxhighlight lang="ecmascriptwren">var shades = ".:!*oe&#\%@"
var light = [30, 30, -50]
 
Line 5,919 ⟶ 6,032:
next n
end sub</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, "Draw a Sphere");
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.DrawSphereWires(.{ .x = 0, .y = 0, .z = 0 }, 2, 20, 20, c.LIME);
}
}
}
</syntaxhighlight>
 
=={{header|zkl}}==
81

edits