Draw a sphere: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 9 users not shown)
Line 21:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V shades = [‘.’, ‘:’, ‘!’, ‘*’, ‘o’, ‘e’, ‘&’, ‘#’, ‘%’, ‘@’]
 
F dotp(v1, v2)
Line 46:
V light = normalize((30.0, 30.0, -50.0))
draw_sphere(20, 4, 0.1, light)
draw_sphere(10, 2, 0.4, light)</langsyntaxhighlight>
 
{{out}}
Line 115:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">INT ARRAY SinTab=[
0 4 9 13 18 22 27 31 36 40 44 49 53 58 62 66 71 75 79 83
88 92 96 100 104 108 112 116 120 124 128 132 136 139 143
Line 176:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Draw_a_sphere.png Screenshot from Atari 8-bit computer]
Line 185:
Uses the Cairo component of GtkAda to create and save as png
[[File:SphereAda.png|thumb|right]]
<langsyntaxhighlight Adalang="ada">with Glib; use Glib;
with Cairo; use Cairo;
with Cairo.Png; use Cairo.Png;
Line 214:
Status_Out := Write_To_Png (Surface, "SphereAda.png");
pragma Assert (Status_Out = Cairo_Status_Success);
end Sphere;</langsyntaxhighlight>
 
{{libheader|Display}}
This uses a very loose binding to SDL as found in any GPS installation. For it to work, you must choose New Project From Templte|Empty Game
 
<syntaxhighlight lang="ada">
<lang Ada>
with Display; use Display;
with Display.Basic; use Display.Basic;
Line 232:
null;
end Main;
</syntaxhighlight>
</lang>
 
=={{header|ALGOL W}}==
{{Trans|AWK}} with some changes inspired by other samples.
<langsyntaxhighlight lang="algolw">begin
% draw a sphere %
% returns the next integer larger than x or x if x is an integer %
Line 319:
drawSphere( 10, 2, 0.4, light, ".:!*oe#%&@" )
end test
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 397:
 
=={{header|ATS}}==
<syntaxhighlight lang="text">
(*
** Solution to Draw_a_sphere.dats
Line 499:
 
(* ****** ****** *)
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<langsyntaxhighlight lang="ahk">#NoEnv
SetBatchLines, -1
#SingleInstance, Force
Line 559:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DRAW_A_SPHERE.AWK
# converted from VBSCRIPT
Line 622:
return(n)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 694:
This is based, but not exactly, on the [http://rosettacode.org/wiki/Draw_a_sphere#Tcl Tcl] implementation.
Thus, the output is almost the same to the output of Tcl implementation below.
<langsyntaxhighlight lang="basic256">clg
color white
rect 0,0,graphwidth, graphheight
Line 700:
color rgb(2*n,2*n,2*n)
circle 150-2*n/3,150-n/2,150-n
next n</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
Using Direct3D.
<langsyntaxhighlight lang="bbcbasic"> MODE 8
INSTALL @lib$+"D3DLIB"
D3DTS_VIEW = 2
Line 822:
SYS "FreeLibrary", d3dx%
END
</syntaxhighlight>
</lang>
{{out}}
[[File:Sphere_BBC.jpeg]]
Line 830:
Some simple 3D objects are built into DarkBASIC. Creating a sphere only takes 1 line:
 
<syntaxhighlight lang ="darkbasic">MAKE OBJECT SPHERE 1,1</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight FreeBASIClang="freebasic">' "\" = a integer division (CPU)
' "/" = a floating point division (FPU)
' the compiler takes care of the conversion between floating point and integer
Line 880:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{works with|FreeBASIC}}
needs #Lang "fblite", #Lang "qb" or #Lang "deprecated" to compile.
<langsyntaxhighlight FreeBASIClang="freebasic">'Sphere for FreeBASIC May 2015
'spherefb4.bas
'Sphere using XPL0 code from rosetacode sphere page
Line 918:
Print "Enter any key to exit "
sleep
END</langsyntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">
<lang lb>
WindowWidth =420
WindowHeight =460
Line 948:
close #w
end
</syntaxhighlight>
</lang>
 
==={{header|Locomotive Basic}}===
Translated from ERRE version, this will print a 39x20 text sphere onscreen.
The variables in line 80 can be used to adjust size (r), spotlight (k), reflective light (ambient).
<langsyntaxhighlight lang="locobasic">
10 MODE 2:s$=".:!*oe&#%@"
20 DIM v(2),vec(2)
Line 990:
3010 IF d<0 THEN d=-d ELSE d=0
3020 RETURN
</syntaxhighlight>
</lang>
 
==={{header|PureBasic}}===
3D Sphere animation.
<langsyntaxhighlight PureBasiclang="purebasic">; Original by Comtois @ 28/03/06
;
; Updated/Formated by Fluid Byte @ March.24,2009
Line 1,244:
SetMeshData(0,#PB_Mesh_Face,*IBuffer,Length)
EndProcedure</langsyntaxhighlight>
[[image:PB_Animated_sphere.png]]
 
===[[QBasic]]===
 
<langsyntaxhighlight QBASIClang="qbasic">SCREEN 13 ' enter high-color graphic mode
 
' sets palette colors B/N
Line 1,267:
' wait until keypress
DO: LOOP WHILE INKEY$ = ""
END</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="runbasic">'Run BASIC White Sphere, Black background
'runbasic.com
graphic #win, 300, 300
Line 1,287:
next X
next Y
render #win</langsyntaxhighlight>
 
<langsyntaxhighlight Runbasiclang="runbasic">'This is a simple Circle
graphic #g, 300, 300 'create a graphic object
#g place(100,100) 'place the drawing pen at 100,100
#g circle(75) 'make a circle with radius 75
render #g 'show it</langsyntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM. A screenshot of the output is [http://www.edmundgriffiths.com/zx81sphere.jpg here].
<langsyntaxhighlight lang="basic">10 LET I=21
20 LET J=2
30 FOR K=-PI TO PI STEP 0.07
Line 1,305:
70 LET I=I-J
80 LET J=J+1
90 IF I>0 THEN GOTO 30</langsyntaxhighlight>
 
=={{header|Batch File}}==
Line 1,311:
Since Batch Files do not support floating point, the input parameters for drawing the sphere are limited to integers only. The ''k'' parameter has been hardcoded to 2. The <code>ambient</code> variable for this code is scaled up 10 times of its value in C implementation. For example, <code>ambient = 0.1</code> in C code corresponds to <code>ambient = 1</code> here.
Lastly, the variables used in calculations are scaled up 100 times of the actual values in C implementation, and then scaled down 100 times back for determination of shades.
<langsyntaxhighlight lang="dos">:: Draw a Sphere Task from Rosetta Code
:: Batch File Implementation
Line 1,318:
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:
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,413:
echo(!line!
)
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre> eeeeeeeeee&&&&&##
Line 1,464:
Also note that the z-coordinate of the light vector is negated at runtime to more closely match the C defaults. This is preferable to making the initial constant negative since negative data values aren't supported across all Befunge implementations.
 
<langsyntaxhighlight lang="befunge">45*65*65*"2"30p20p10p::00p2*40p4*5vv<
>60p140g->:::*00g50g*60g40g-:*-\-v0>1
^_@#`\g0<|`\g04:+1, <*84$$_v#`\0:<>p^
Line 1,471:
^>#0 *#12#<0g:^>+::"~~"90g*80g+*70gv|
g-10g*+:9**00gv|!*`\2\`-20::/2-\/\+<>
%#&eo*!:..^g05<>$030g-*9/\20g*+60g40^</langsyntaxhighlight>
 
{{out}}
Line 1,517:
=={{header|Brlcad}}==
 
<langsyntaxhighlight lang="brlcad">opendb balls.g y # Create a database to hold our shapes
units cm # Set the unit of measure
in ball.s sph 0 0 0 3 # Create a sphere of radius 3 cm named ball.s with its centre at 0,0,0 </langsyntaxhighlight>
 
=={{header|C}}==
The lighting calculation is somewhere between crude and bogus, but hey, I'm shading it with ASCII characters, don't expect too much.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,579:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> #############%%%%
Line 1,645:
===Fun with 3D noise texture===
[[file:sphere-perlin.png]]
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Line 1,806:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|C}}
<langsyntaxhighlight lang="java">using System;
 
namespace Sphere {
Line 1,860:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{libheader|Qt}}
<langsyntaxhighlight lang="cpp">// Based on https://www.cairographics.org/samples/gradient/
 
#include <QImage>
Line 1,900:
image.save("sphere.png");
return 0;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Draw a sphere cpp.png]]
See: [https://slack-files.com/T0CNUL56D-F017MAG46BH-6af262a360 sphere.png] (offsite PNG image)
 
=={{header|Clojure}}==
{{libheader|quil}}
<langsyntaxhighlight lang="clojure">
(use 'quil.core)
 
Line 1,928:
:draw draw
:renderer :opengl)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,938:
Saved as a png file and rendered in a X-Window. Unfortunately the file upload isn't working anymore for like four years so I cannot show my results directly.
 
<langsyntaxhighlight lang="lisp">;; * Loading the cairo bindings
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload '("cl-cairo2" "cl-cairo2-xlib")))
Line 1,982:
(set-source pat)
(arc *middle* *middle* 180 0 (* 2 pi))
(fill-path))))</langsyntaxhighlight>
 
=={{header|ContextFree}}==
<syntaxhighlight lang="contextfree">
<lang contextFree>
startshape SPHERE
 
Line 1,992:
SPHERE[x 0.1% y 0.1%s 0.99 0.99 b 0.05]
}
</syntaxhighlight>
</lang>
 
=={{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}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.numeric;
 
alias V3 = double[3];
Line 2,035 ⟶ 2,053:
drawSphere(20, 4, 0.1);
drawSphere(10, 2, 0.4);
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 2,044 ⟶ 2,062:
'''Steps:''' Run the CMD Windows shell. Then follow this path to setup the new width: '''Main Menu-> Properties -> Layout -> Window Size -> Width'''.
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program DrawASphere;
 
Line 2,115 ⟶ 2,133:
Readln;
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,183 ⟶ 2,201:
[[image:DWScript-sphere.pbm.png|thumb|right|PBM output magnified 5 times]]
{{trans|C}} but adapted to spit out a [[wp:Netpbm_format|PGM]] image
<langsyntaxhighlight lang="delphi">
type
TFloat3 = array[0..2] of Float;
Line 2,239 ⟶ 2,257:
normalize(light);
drawSphere(19, 4, 0.1);
</syntaxhighlight>
</lang>
 
=={{header|Emacs Lisp}}==
{{trans|Go}}
<syntaxhighlight lang="lisp">; Draw a sphere
 
(defun normalize (v)
"Normalize a vector."
(setq invlen (/ 1.0 (sqrt (dot v v))))
(mapcar (lambda (x) (* invlen x)) v))
 
(defun dot (v1 v2)
"Dot product of two vectors."
(+ (* (car v1) (car v2))
(* (cadr v1) (cadr v2))
(* (caddr v1) (caddr v2))))
 
(defun make-array (size)
"Create an empty array with size*size elements."
(setq m-array (make-vector size nil))
(dotimes (i size)
(setf (aref m-array i) (make-vector size 0)))
m-array)
 
(defun pic-lines (arr size)
"Turn array into a string."
(setq all "")
(dotimes (y size)
(setq line "")
(dotimes (x size)
(setq line (concat line (format "%i \n" (elt (elt arr y) x)))))
(setq all (concat all line "\n")))
all)
 
(defun pic-show (arr size)
"Convert size*size array to grayscale PBM image and show it."
(insert-image (create-image (concat (format "P2
%i %i 255\n" size size) (pic-lines arr size)) 'pbm t)))
 
(defun sphere (size k amb dir)
"Draw a sphere."
(let ((arr (make-array size))
(ndir (normalize dir))
(r (/ size 2)))
(dotimes (yp size)
(dotimes (xp size)
(setq x (- xp r))
(setq y (- yp r))
(setq z (- (* r r) (* x x) (* y y)))
(if (>= z 0)
(let* ((vec (normalize (list x y (sqrt z))))
(s (max 0 (dot vec ndir)))
(lum (max 0 (min 255 (* 255 (+ amb (expt s k))
(/ (1+ amb)))))))
(setf (elt (elt arr yp) xp) lum)))))
(pic-show arr size)))
 
(sphere 200 1.5 0.2 '(-30 -30 50))</syntaxhighlight>
 
=={{header|ERRE}}==
Using ASCII art: output is written to 'SPHERE.PRN' sequential file.
<langsyntaxhighlight ERRElang="erre">PROGRAM SPHERE
 
CONST SHADES$=".:!*oe&#%@"
Line 2,297 ⟶ 2,372:
CLOSE(1)
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre> !::::::!!!**o
Line 2,363 ⟶ 2,438:
</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}}==
{{libheader|raylib}}
{{works with|Factor|0.99 2020-03-02}}
<langsyntaxhighlight lang="factor">USING: classes.struct kernel raylib.ffi ;
640 480 "sphere" init-window
Line 2,388 ⟶ 2,478:
end-mode-3d
end-drawing
] until drop close-window</langsyntaxhighlight>
{{out}}
[https://i.imgur.com/FXHkZm6.png]
Line 2,399 ⟶ 2,489:
 
===ASCII output===
<langsyntaxhighlight lang="forth">: 3dup 2 pick 2 pick 2 pick ;
 
: sqrt ( u -- sqrt ) ( Babylonian method )
Line 2,440 ⟶ 2,530:
 
20 draw
10 draw</langsyntaxhighlight>
 
{{out}}
Line 2,506 ⟶ 2,596:
===PGM output===
The same program as the ASCII one is translated to produce a PGM portable pixmap image file.
<langsyntaxhighlight lang="forth">: 3dup 2 pick 2 pick 2 pick ;
 
: sqrt ( u -- sqrt ) ( Babylonian method )
Line 2,553 ⟶ 2,643:
1000 +loop drop image-close ;
 
200 draw</langsyntaxhighlight>
 
=={{header|Frink}}==
This program not only draws a sphere and renders it onscreen projected on the x,y, and z axes but also outputs a <CODE>.stl</CODE> file for 3-D printing or display in a 3-D modeling package like MeshLab! Frink has [https://frinklang.org/3d/frink/graphics/package-summary.html built-in routines for 3-D modeling] and can emit STL files or Wavefront OBJ files natively! Frink will let you print a sphere that you can hold in your hand!
<langsyntaxhighlight lang="frink">res = 254 / in
v = callJava["frink.graphics.VoxelArray", "makeSphere", [1/2 inch res]]
 
Line 2,569 ⟶ 2,659:
w.println[v.toSTLFormat["sphere", 1/(res mm)]]
w.close[]
println["done."]</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">
_window = 1
include "Tlbx agl.incl"
begin enum output 1
include "Tlbx glut.incl"
_sphereImageView
end enum
 
void local fn BuildWindow
output file "Rotating Sphere"
CGRect r = fn CGRectMake( 0, 0, 400, 400 )
window _window, @"Rosetta Code Sphere", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 20, 20, 360, 360 )
imageview _sphereImageView, YES, , r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameNone, _window
end fn
 
local fn SphereImageWithCIFilter( imageDimension as NSUInteger, sphereColor as ColorRef, backgroundColor as ColorRef, radiusBlur as float, radiusSphere as float ) as ImageRef
local fn SphereDraw
CIVectorRef civ = fn CIVectorWithXY( imageDimension/2, imageDimension/2 )
'~'1
CIFilterRef filter = fn CIFilterWithName( @"CIRadialGradient" )
begin globals
CIFilterSetDefaults( filter )
dim as double  sRotation // 'static' var
ObjectSetValueForKey( filter, civ, @"inputCenter" )
end globals
ObjectSetValueForKey( filter, fn NumberWithFloat( radiusBlur ), @"inputRadius0" )
 
ObjectSetValueForKey( filter, fn NumberWithFloat( radiusSphere ), @"inputRadius1" )
// Speed of rotation
ObjectSetValueForKey( filter, fn CIColorWithCGColor( fn ColorCGColor( sphereColor ) ), @"inputColor0" )
sRotation += 2.9
ObjectSetValueForKey( filter, fn CIColorWithCGColor( fn ColorCGColor( backgroundColor ) ), @"inputColor1" )
glMatrixMode( _GLMODELVIEW )
 
ImageRef resultImage = fn ImageWithSize( fn CGSizeMake( imageDimension, imageDimension ) )
glLoadIdentity()
ImageLockFocus( resultImage )
 
CIImageDrawAtPoint( fn CIFilterOutputImage( filter ), CGPointZero, fn CGRectMake( 0, 0, imageDimension, imageDimension ), NSCompositeDestinationAtop, 1.0 )
// Position parameters: x axis, y axis, z axis
ImageUnlockFocus( resultImage )
// Set to center of screen:
end fn = resultImage
glTranslated( 0.0, 0.0, 0.0 )
 
// Rotation (wobble) parameters: angle, x, y
glRotated( sRotation, -0.45, -0.8, -0.6 )
 
// Set color of sphere's wireframe
glColor3d( 1.0, 0.0, 0.3 )
 
// Set width of sphere's wireframe lines
glLineWidth( 1.5 )
 
// Apply above to GLUT's built-in sphere wireframe
// Size & frame parameters: radius, slices, stack
fn glutWireSphere( 0.8, 25, 25 )
 
local fn BuildSphere
ImageRef sphereImage = fn SphereImageWithCIFilter( 340, fn ColorWithRGB( 0.988, 0.335, 0.176, 1.0 ), fn ColorBlack, 0.0, 125.0 )
ImageViewSetImage( _sphereImageView, sphereImage )
end fn
 
fn BuildWindow
// main program
fn BuildSphere
dim as GLint           attrib(2)
dim as CGrafPtr        port
dim as AGLPixelFormat  fmt
dim as AGLContext      glContext
dim as EventRecord     ev
dim as GLboolean       yesOK
 
// Make a window
window 1, @"Rotating Sphere", (0,0) - (500,500)
 
// Minimal setup of OpenGL
attrib(0) = _AGLRGBA
attrib(1) = _AGLDOUBLEBUFFER
attrib(2) = _AGLNONE
 
fmt = fn aglChoosePixelFormat( 0, 0, attrib(0) )
glContext = fn aglCreateContext( fmt, 0 )
aglDestroyPixelFormat( fmt )
 
// Set the FB window as port for drawing
port = window( _wndPort )
yesOK = fn aglSetDrawable( glContext, port )
yesOK = fn aglSetCurrentContext( glContext )
 
// Background color: red, green, blue, alpha
glClearColor( 0.0, 0.0, 0.0, 0.0 )
 
// 60/s HandleEvents Trigger
poke long event - 8, 1
do
// Clear window
glClear( _GLCOLORBUFFERBIT )
// Run animation
fn SphereDraw
aglSwapBuffers( glContext )
HandleEvents
</syntaxhighlight>
until gFBquit
{{output}}
</lang>
[[File:Rosetta Code Sphere.png]]
 
=={{header|Go}}==
Line 2,653 ⟶ 2,709:
{{trans|C}}
Using image library rather than ASCII art.
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,720 ⟶ 2,776:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
[[File:Sphere_Haskell.png|thumb|right]]
<langsyntaxhighlight lang="haskell">import Graphics.Rendering.OpenGL.GL
import Graphics.UI.GLUT.Objects
import Graphics.UI.GLUT
Line 2,771 ⟶ 2,827:
setMaterial
displayCallback $= display
mainLoop</langsyntaxhighlight>
 
=== ASCII art ===
{{trans|Python}}
 
<langsyntaxhighlight lang="haskell">import Data.List (genericLength)
 
shades = ".:!*oe%#&@"
Line 2,791 ⟶ 2,847:
else ' '
| y <- map (/2.12) [- 2*r - 0.5 .. 2*r + 0.5] ]
| x <- [ - r - 0.5 .. r + 0.5] ]</langsyntaxhighlight>
 
<pre>λ> putStrLn $ sphere 10 4 0.1 (normalize [30,30,-50])
Line 2,822 ⟶ 2,878:
Unicon provides a built-in interface to openGL including some higher level abstractions (for more information see [[Icon%2BUnicon/Intro#Graphics.2C_Network_Messaging.2C_etc.|Unicon Technical References, 3D Graphics]]). The example below draws a blue sphere on a black background and waits for input to quit.[[File:Sphere_unicon.PNG|thumb|Unicon Sphere]]
 
<langsyntaxhighlight Uniconlang="unicon">procedure main()
W := open("Demo", "gl", "size=400,400", "bg=black") | stop("can't open window!")
WAttrib(W, "slices=40", "rings=40", "light0=on, ambient white; diffuse gold; specular gold; position 5, 0, 0" )
Line 2,828 ⟶ 2,884:
DrawSphere(W, 0, 0, -5, 1)
Event(W)
end</langsyntaxhighlight>
<div style="border: 1px solid #000000; overflow: auto; width: 100%"></div>
 
Line 2,846 ⟶ 2,902:
But bringing up the example with a line of code is trivial enough:
 
<langsyntaxhighlight lang="j">load '~system/examples/graphics/opengl/simple/sphere.ijs'</langsyntaxhighlight>
 
(Note that this example has been removed from recent versions of J, but still works for J version 5 and version 6.)
Line 2,855 ⟶ 2,911:
Here's a version using raytracing computed in J. luminosity is an array of luminosity values with theoretical maximum 1 and minimum 0, and viewmat is used to display this.
 
<langsyntaxhighlight lang="j">'R k ambient' =. 10 2 0.4
light =. (% +/&.:*:) 30 30 _50
pts =. (0&*^:(0={:))@:(,,(0>.(*:R)-+)&.*:)"0/~ i:15j200
Line 2,862 ⟶ 2,918:
load 'viewmat'
torgb =. 256 #. [: <. 255 255 255 *"1 0 ]
'rgb' viewmat torgb luminosity</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">public class Sphere{
static char[] shades = {'.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@'};
 
Line 2,908 ⟶ 2,964:
drawSphere(10, 2, .4);
}
}</langsyntaxhighlight>
{{out}}
<pre> &&&&&&&&&&#######
Line 2,979 ⟶ 3,035:
This Javascript entry uses an HTML wrapper to offer easy running and some interactivity. It is made as such, though, that the entire HTML wrapper can be removed (except for a canvas with id <code>c</code>) and still work. If you remove the HTML, call the <code>draw_sphere</code> function to draw the thing.
 
<langsyntaxhighlight lang="javascript"><!DOCTYPE html>
<html>
<head>
Line 3,040 ⟶ 3,096:
<canvas id="c">Unsupportive browser...</canvas><br>
</body>
</html></langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.4}}
The approach adopted here is to generate an SVG file, which may then be viewed, for example, in a web browser.
<langsyntaxhighlight lang="jq">def svg:
"<svg width='100%' height='100%' version='1.1'
xmlns='http://www.w3.org/2000/svg'
Line 3,063 ⟶ 3,119:
 
def sphere(cx; cy; r; gradientId):
"<circle fill='url(#\(gradientId))' cx='\(cx)' cy='\(cy)' r='\(r)' />" ;</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="jq">def draw_sphere:
svg,
"<title>Teal sphere</title>",
Line 3,074 ⟶ 3,130:
"</svg>" ;
draw_sphere</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -r -n -f spheres.jq > spheres.svg</langsyntaxhighlight>
 
One way to view the output as an image is to point your browser to the generated SVG.
Line 3,083 ⟶ 3,139:
===ASCII Text===
{{trans|C}}
<langsyntaxhighlight lang="julia">function draw_sphere(r, k, ambient, light)
shades = ('.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@')
for i in floor(Int, -r):ceil(Int, r)
Line 3,110 ⟶ 3,166:
draw_sphere(20, 4, 0.1, light)
draw_sphere(10, 2, 0.4, light)
</syntaxhighlight>
</lang>
===Graphical===
<langsyntaxhighlight lang="julia"># run from REPL
 
using Makie
Line 3,125 ⟶ 3,181:
 
surface(x, y, z, backgroundcolor = :black, show_axis = false)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
const val shades = ".:!*oe&#%@"
Line 3,175 ⟶ 3,231:
drawSphere(20.0, 4.0, 0.1)
drawSphere(10.0, 2.0, 0.4)
}</langsyntaxhighlight>
 
{{out}}
Line 3,243 ⟶ 3,299:
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">----------------------------------------
-- Draw a circle
-- @param {image} img
Line 3,258 ⟶ 3,314:
props[#color] = drawColor
img.draw(x-r, y-r, x+r, y+r, props)
end</langsyntaxhighlight>
 
=={{header|Logo}}==
Line 3,265 ⟶ 3,321:
{{works with|MSWlogo}}
 
<langsyntaxhighlight lang="logo">to sphere :r
cs perspective ht ;making the room ready to use
repeat 180 [polystart circle :r polyend down 1]
polyview
end</langsyntaxhighlight>
 
=={{header|Lua}}==
{{trans|C}}
{{works with|Lua|5.1.4}}
<langsyntaxhighlight Lualang="lua">require ("math")
 
shades = {'.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@'}
Line 3,310 ⟶ 3,366:
 
draw_sphere (20, 4, 0.1)
draw_sphere (10, 2, 0.4)</langsyntaxhighlight>
{{out}}
<pre> &&&&&&&&&&&&#####
Line 3,376 ⟶ 3,432:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Er$="Pset is a new statement"
Line 3,436 ⟶ 3,492:
}
Checkit
</syntaxhighlight>
</lang>
 
[[https://2.bp.blogspot.com/-ZWy2xDxXbzg/W98lAuNSY9I/AAAAAAAAHZ0/DFYluvWtz_cwwAUKfblujnW6mTC5XVs1QCLcBGAs/s1600/sphere.png]image]
Line 3,442 ⟶ 3,498:
=={{header|Maple}}==
[[File:Sphere_Maple.png|thumb]]
<langsyntaxhighlight lang="maple">plots[display](plottools[sphere](), axes = none, style = surface);</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has many 3D drawing capabilities. To create a sphere with radius one centered at (0,0,0):
<langsyntaxhighlight Mathematicalang="mathematica">Graphics3D[Sphere[{0,0,0},1]]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
To create the unit sphere:
<syntaxhighlight lang MATLAB="matlab">figure; sphere</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* Two solutions */
plot3d(1, [theta, 0, %pi], [phi, 0, 2 * %pi],
[transform_xy, spherical_to_xyz], [grid, 30, 60],
Line 3,463 ⟶ 3,519:
sin(phi)*sin(theta),
cos(theta),
theta, 0, %pi, phi, 0, 2 * %pi))$</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import math
 
type Point = tuple[x,y,z: float]
Line 3,497 ⟶ 3,553:
 
drawSphere 20, 4.0, 0.1
drawSphere 10, 2.0, 0.4</langsyntaxhighlight>
{{out}}
<pre> &&&&&&&&&&#######
Line 3,563 ⟶ 3,619:
=={{header|Ol}}==
{{libheader|OpenGL}}
<langsyntaxhighlight lang="scheme">
(import (lib gl))
(import (OpenGL version-1-0))
Line 3,580 ⟶ 3,636:
(gluSphere quadric 0.4 32 10)
))
</syntaxhighlight>
</lang>
 
=={{header|Openscad}}==
Drawing a sphere is easy in openscad:
 
<langsyntaxhighlight lang="openscad">// This will produce a sphere of radius 5
sphere(5);</langsyntaxhighlight>
 
[[Category:Geometric Primitives]]
Line 3,592 ⟶ 3,648:
=={{header|OxygenBasic}}==
Using an OpenGl-based console
<syntaxhighlight lang="text">
% Title "Sphere"
'% Animated
Line 3,610 ⟶ 3,666:
 
EndScript
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
Line 3,621 ⟶ 3,677:
This produces a PGM image which can't be uploaded on rosettacode at the moment. It looks similar as the Raku solution, though.
 
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 3,671 ⟶ 3,727:
},
q{""} => sub { sprintf "Vector:[%s]", join ' ', @{shift()} };
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 3,679 ⟶ 3,735:
Sphere will resize to match the window.
You can run this online [http://phix.x10.mx/p2js/drawsphere.htm here]. Note fullscreen redraw can be quite slow.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Draw_a_sphere.exw
Line 3,766 ⟶ 3,822:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
{{libheader|GLUT}}
This is for the 64-bit version.
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/openGl.l")
 
(glutInit)
Line 3,796 ⟶ 3,852:
# Exit upon mouse click
(mouseFunc '((Btn State X Y) (bye)))
(glutMainLoop)</langsyntaxhighlight>
 
{{trans|C}}
 
<langsyntaxhighlight PicoLisplang="picolisp">(scl 24)
 
(setq *Shades
Line 3,859 ⟶ 3,915:
(setq *Light (normalize *Light))
(drawSphere 20.0 4 0.1)
(drawSphere 10.0 2 0.4)</langsyntaxhighlight>
{{out}}
<pre> ##############%%%
Line 3,929 ⟶ 3,985:
=={{header|PostScript}}==
Gradient filled circle:
<langsyntaxhighlight PostScriptlang="postscript">%!PS-Adobe-3.0
%%BoundingBox 0 0 300 300
 
Line 3,952 ⟶ 4,008:
showpage
%%EOF
</syntaxhighlight>
</lang>
 
=={{header|POV-Ray}}==
Line 3,958 ⟶ 4,014:
This is what POVray was made for. An example with a sky, surface and transparency:
 
<syntaxhighlight lang="povray">
<lang POVray>
camera { location <0.0 , .8 ,-3.0> look_at 0}
Line 3,974 ⟶ 4,030:
interior { ior 1.5}
}
</syntaxhighlight>
</lang>
 
Yields this:
Line 3,983 ⟶ 4,039:
3D rendering is built into Processing.
 
<langsyntaxhighlight lang="java">void setup() {
size(500, 500, P3D);
}
Line 3,995 ⟶ 4,051:
// draw sphere
sphere(200);
}</langsyntaxhighlight>
A sphere build from triangles (to for instance allow distortion)
<langsyntaxhighlight lang="java">float rotX, rotY;
 
PVector[][] sphere;
Line 4,041 ⟶ 4,097:
rotY -= (mouseX - pmouseX) * 0.01;
rotX -= (mouseY - pmouseY) * 0.01;
}</langsyntaxhighlight>
 
=={{header|Python}}==
===Ascii-Art===
{{trans|C}}
<langsyntaxhighlight lang="python">import math
 
shades = ('.',':','!','*','o','e','&','#','%','@')
Line 4,077 ⟶ 4,133:
light = normalize((30,30,-50))
draw_sphere(20,4,0.1, light)
draw_sphere(10,2,0.4, light)</langsyntaxhighlight>
{{out}}
<pre> &&&&&&&&&&######
Line 4,147 ⟶ 4,203:
This code contains unnecessary functions which are part of a 3D graphics library I wrote. <br>
Uses Pygame and Python 3.2.2
<langsyntaxhighlight lang="python">
import pygame
from pygame.locals import *
Line 4,154 ⟶ 4,210:
import random
import math
</syntaxhighlight>
</lang>
<langsyntaxhighlight lang="python">
class Tricubic:
def __init__(self,pts):
Line 4,927 ⟶ 4,983:
if event.type == KEYDOWN:
pass
</syntaxhighlight>
</lang>
 
==={{libheader|VPython}}===
{{works with|Python|2.7.5}}
'''Short version''':
<langsyntaxhighlight lang="python">from visual import *
scene.title = "VPython: Draw a sphere"
sphere() # using defaults, see http://www.vpython.org/contents/docs/defaults.html </langsyntaxhighlight>
 
'''Regular version''', with some window-dressing:
<langsyntaxhighlight lang="python">
from __future__ import print_function, division
from visual import *
Line 4,960 ⟶ 5,016:
rate(100)
pass # no animation in this demo
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 4,968 ⟶ 5,024:
Using the Typed Racket language with the plot library:
 
<langsyntaxhighlight lang="racket">
#lang typed/racket
 
(require plot/typed)
(plot3d (polar3d (λ (θ ρ) 1)) #:altitude 25)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 4,985 ⟶ 5,041:
 
[[File:Sphere-perl6.png|thumb]]
<syntaxhighlight lang="raku" perl6line>my $width = my $height = 255; # must be odd
 
my @light = normalize([ 3, 2, -5 ]);
Line 5,022 ⟶ 5,078:
}
flat |@pixels.map: *.list;
}</langsyntaxhighlight>
 
===Cairo graphics library===
<syntaxhighlight lang="raku" perl6line>use Cairo;
 
given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
Line 5,055 ⟶ 5,111:
};
.write_png('sphere2-perl6.png');
}</langsyntaxhighlight>
See [https://github.com/thundergnat/rc/blob/master/img/sphere2-perl6.png sphere2-perl6.png] (offsite .png image)
 
Line 5,065 ⟶ 5,121:
 
Programming note: &nbsp; the output will appear slightly different when executed on an EBCDIC machine &nbsp; (due to different dithering characters).
<langsyntaxhighlight lang="rexx">/*REXX program expresses a lighted sphere with simple characters used for shading. */
call drawSphere 19, 4, 2/10, '30 30 -50' /*draw a sphere with a radius of 19. */
call drawSphere 10, 2, 4/10, '30 30 -50' /* " " " " " " " ten. */
Line 5,099 ⟶ 5,155:
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g= g*.5'e'_%2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input and executed on an '''ASCII''' machine:}}
(Shown at &nbsp; <big>'''<sup>1</sup>/<sub>2</sub>'''</big> &nbsp; size.)
Line 5,169 ⟶ 5,225:
Shoes comes with this sample program.
[[File:sphere.shoes.png|thumb|right]]
<langsyntaxhighlight lang="ruby">Shoes.app :width => 500, :height => 500, :resizable => false do
image 400, 470, :top => 30, :left => 50 do
nostroke
Line 5,196 ⟶ 5,252:
end
end
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{trans|Go}}
<langsyntaxhighlight lang="rust">// [dependencies]
// image = "0.23"
 
Line 5,261 ⟶ 5,317:
Err(error) => eprintln!("{}", error),
}
}</langsyntaxhighlight>
 
{{out}}
[[Media:Draw a sphere rust.png]]
See: [https://slack-files.com/T0CNUL56D-F018F40PPC1-a6a001141d sphere_rust.png] (offsite PNG image)
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Sphere extends App {
private val (shades, light) = (Seq('.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@'), Array(30d, 30d, -50d))
 
Line 5,300 ⟶ 5,356:
drawSphere(10, 2, .4)
 
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/uSm8bJ9/0 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/TtVHUp3aS0eDEB6YCW4gKg Scastie (JVM)].
 
Line 5,306 ⟶ 5,362:
{{trans|Raku}}
Produces a PGM image.
<langsyntaxhighlight lang="ruby">func normalize (vec) { vec »/» (vec »*« vec -> sum.sqrt) }
func dot (x, y) { -(x »*« y -> sum) `max` 0 }
 
Line 5,338 ⟶ 5,394:
out.say("P5\n#{x} #{y}\n#{depth}") # .pgm header
out.print(draw_sphere((x-1)/2, .9, .2).map{.chr}.join)
out.close</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 5,346 ⟶ 5,402:
{{works with|Smalltalk/X}}
although there is a Point3 class in some loadable library, here is some self contained code, defining a local anon Point3D class.
<syntaxhighlight lang="smalltalk">
<lang Smalltalk>
Point3D :=
Point subclass:#Point3D
Line 5,410 ⟶ 5,466:
 
main value.
</syntaxhighlight>
</lang>
[[file:sphere-smalltalk.png]]
 
Line 5,419 ⟶ 5,475:
=={{header|Swift}}==
In Playground for example:
<syntaxhighlight lang="swift">
<lang Swift>
class Sphere: UIView{
Line 5,451 ⟶ 5,507:
 
var s = Sphere(frame: CGRectMake(0, 0, 200, 200))
</syntaxhighlight>
</lang>
 
In SwiftUI:
<syntaxhighlight lang="swift">
<lang Swift>
struct ContentView: View {
 
Line 5,468 ⟶ 5,524:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
Line 5,474 ⟶ 5,530:
Assuming the task is to draw a <i>likeness</i> of a sphere, this would usually do:
 
<langsyntaxhighlight Tcllang="tcl">proc grey {n} {format "#%2.2x%2.2x%2.2x" $n $n $n}
 
pack [canvas .c -height 400 -width 640 -background white]
Line 5,482 ⟶ 5,538:
.c create arc [expr {100+$i/5}] [expr {50+$i/5}] [expr {400-$i/1.5}] [expr {350-$i/1.5}] \
-start 0 -extent 359 -fill $h -outline $h
}</langsyntaxhighlight>
Results in this image:
 
Line 5,492 ⟶ 5,548:
The PGF <code>shadings</code> library includes a "ball" for a 3-D style highlight.
 
<langsyntaxhighlight LaTeXlang="latex">\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shadings}
Line 5,499 ⟶ 5,555:
\shade[ball color=black] (0,0) circle (4);
\end{tikzpicture}
\end{document}</langsyntaxhighlight>
 
=={{header|Turing}}==
Translated and optimized from the Algol W solution and others posted here.
 
<langsyntaxhighlight Turinglang="turing">% Draw a sphere in ASCII art in Turing
% Light intensity to character map
Line 5,561 ⟶ 5,617:
% Draw some spheres
drawSphere (20, 4, lightsource, brightness)
drawSphere (15, 10, lightsource, brightness)</langsyntaxhighlight>
 
{{Out}}
Line 5,640 ⟶ 5,696:
=={{header|VBScript}}==
{{trans|C}}
<langsyntaxhighlight lang="vb">shades = Array(".", ":", "!", "*", "o", "e", "&", "#", "%", "@")
light = Array(30, 30, -50)
 
Line 5,690 ⟶ 5,746:
Normalize light
DrawSphere 20, 4, 0.1
DrawSphere 10,2,0.4</langsyntaxhighlight>
{{Out}}
<pre> &&&&&&&&&&#######
Line 5,753 ⟶ 5,809:
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}}
<langsyntaxhighlight ecmascriptlang="wren">var shades = ".:!*oe&#\%@"
var light = [30, 30, -50]
 
Line 5,793 ⟶ 5,892:
normalize.call(light)
drawSphere.call(20, 4, 0.1)
drawSphere.call(10, 2, 0.4)</langsyntaxhighlight>
 
{{out}}
Line 5,862 ⟶ 5,961:
=={{header|XPL0}}==
[[File:SphereXPL0.png|right]]
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
def R=100, R2=R*R; \radius, in pixels; radius squared
def X0=640/2, Y0=480/2; \coordinates of center of screen
Line 5,879 ⟶ 5,978:
repeat until KeyHit; \wait for keystroke
SetVid($03); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">ancho = 640 : alto = 480
open window 640,480
backcolor 16,16,16
Line 5,895 ⟶ 5,994:
fill circle ancho/2-2*n/3, alto/2-n/2, 150-n
next n
end sub</langsyntaxhighlight>
 
=={{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}}==
Line 5,901 ⟶ 6,040:
{{trans|XPL0}}
[[File:Sphere.zkl.jpg|200px|thumb|right]]
<langsyntaxhighlight lang="zkl">img:=PPM(640,480);
R:=100; R2:=R*R; //radius, in pixels; radius squared
X0:=640/2; Y0:=480/2; //coordinates of center of screen
Line 5,915 ⟶ 6,054:
}
}
img.write(File("foo.ppm","wb"));</langsyntaxhighlight>
The radius of 100 is the max before the color calculation overflows 24 bits so for a radius (R) of, say 200, use
<langsyntaxhighlight lang="zkl">img[X+X0,Y+Y0]=C*140+C;</langsyntaxhighlight>
Perhaps a more useful solution is to use GnuPlot (I grabbed the code from http://ayapin-film.sakura.ne.jp/Gnuplot/):
[[File:GnuplotSphere.zkl.png|250px|thumb|right]]
<langsyntaxhighlight lang="zkl">#<<<
cmd:=0'|
set term wxt
Line 5,942 ⟶ 6,081:
gnuplot:=System.popen("gnuplot","w");
gnuplot.write(cmd); gnuplot.flush();
ask("Hit return to finish"); gnuplot.close();</langsyntaxhighlight>
Where "term wxt" is X11 on my Linux box. A window pops up and stays until the pipe is closed.
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight lang="zxbasic">1 REM fast
50 REM spheer with hidden lines and rotation
100 CLS
Line 5,997 ⟶ 6,136:
1050 LET yy=bx*x+by*y+bz*z
1060 LET zz=cx*x+cy*y+cz*z
1070 RETURN</langsyntaxhighlight>
 
[[Category:Geometry]]
9,476

edits