Draw a sphere: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(14 intermediate revisions by 7 users not shown)
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,903:
 
{{out}}
[[Media:Draw a sphere cpp.png]]
See: [https://slack-files.com/T0CNUL56D-F017MAG46BH-6af262a360 sphere.png] (offsite PNG image)
 
=={{header|Clojure}}==
Line 1,993:
}
</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,420 ⟶ 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}}==
Line 2,630 ⟶ 2,663:
=={{header|FutureBasic}}==
<syntaxhighlight 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
until gFBquit
</syntaxhighlight>
{{output}}
[[File:Rosetta Code Sphere.png]]
 
=={{header|Go}}==
Line 5,321 ⟶ 5,320:
 
{{out}}
[[Media:Draw a sphere rust.png]]
See: [https://slack-files.com/T0CNUL56D-F018F40PPC1-a6a001141d sphere_rust.png] (offsite PNG image)
 
=={{header|Scala}}==
Line 5,810 ⟶ 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}}
<syntaxhighlight lang="ecmascriptwren">var shades = ".:!*oe&#\%@"
var light = [30, 30, -50]
 
Line 5,953 ⟶ 5,995:
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}}==
9,476

edits