Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
54
Task/Draw-a-sphere/ERRE/draw-a-sphere.erre
Normal file
54
Task/Draw-a-sphere/ERRE/draw-a-sphere.erre
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
PROGRAM SPHERE
|
||||
|
||||
CONST SHADES$=".:!*oe&#%@"
|
||||
|
||||
DIM LIGHT[2],X[2],Y[2],V[2],VEC[2]
|
||||
|
||||
PROCEDURE DOT(X[],Y[]->D)
|
||||
D=X[0]*Y[0]+X[1]*Y[1]+X[2]*Y[2]
|
||||
IF D<0 THEN D=-D ELSE D=0 END IF
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE NORMALIZE(V[]->V[])
|
||||
LUNG=SQR(V[0]*V[0]+V[1]*V[1]+V[2]*V[2])
|
||||
V[0]=V[0]/LUNG
|
||||
V[1]=V[1]/LUNG
|
||||
V[2]=V[2]/LUNG
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE PDRAW(R,K,AMBIENT)
|
||||
FOR I=INT(-R) TO INT(R) DO
|
||||
X=I+0.5
|
||||
FOR J=INT(-2*R) TO INT(2*R) DO
|
||||
Y=J/2+0.5
|
||||
IF (X*X+Y*Y<=R*R) THEN
|
||||
VEC[0]=X
|
||||
VEC[1]=Y
|
||||
VEC[2]=SQR(R*R-X*X-Y*Y)
|
||||
NORMALIZE(VEC[]->VEC[])
|
||||
DOT(LIGHT[],VEC[]->D)
|
||||
B=D^K+AMBIENT
|
||||
INTENSITY%=(1-B)*(LEN(SHADES$)-1)
|
||||
IF (INTENSITY%<0) THEN INTENSITY%=0 END IF
|
||||
IF (INTENSITY%>=LEN(SHADES$)-1) THEN
|
||||
INTENSITY%=LEN(SHADES$)-2
|
||||
END IF
|
||||
PRINT(#1,MID$(SHADES$,INTENSITY%+1,1);)
|
||||
ELSE
|
||||
PRINT(#1,(" ");)
|
||||
END IF
|
||||
END FOR
|
||||
PRINT(#1,)
|
||||
END FOR
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
LIGHT[]=(30,30,-50)
|
||||
OPEN("O",1,"SPHERE.PRN")
|
||||
NORMALIZE(LIGHT[]->LIGHT[])
|
||||
PDRAW(10,2,0.4)
|
||||
|
||||
PRINT(#1,STRING$(79,"="))
|
||||
PDRAW(20,4,0.1)
|
||||
CLOSE(1)
|
||||
END PROGRAM
|
||||
74
Task/Draw-a-sphere/FutureBasic/draw-a-sphere.futurebasic
Normal file
74
Task/Draw-a-sphere/FutureBasic/draw-a-sphere.futurebasic
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
include "Tlbx agl.incl"
|
||||
include "Tlbx glut.incl"
|
||||
|
||||
output file "Rotating Sphere"
|
||||
|
||||
local fn SphereDraw
|
||||
'~'1
|
||||
begin globals
|
||||
dim as double sRotation // 'static' var
|
||||
end globals
|
||||
|
||||
// Speed of rotation
|
||||
sRotation += 2.9
|
||||
glMatrixMode( _GLMODELVIEW )
|
||||
|
||||
glLoadIdentity()
|
||||
|
||||
// Position parameters: x axis, y axis, z axis
|
||||
// Set to center of screen:
|
||||
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 )
|
||||
|
||||
end fn
|
||||
|
||||
// main program
|
||||
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
|
||||
16
Task/Draw-a-sphere/Lingo/draw-a-sphere.lingo
Normal file
16
Task/Draw-a-sphere/Lingo/draw-a-sphere.lingo
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
----------------------------------------
|
||||
-- Draw a circle
|
||||
-- @param {image} img
|
||||
-- @param {integer} x
|
||||
-- @param {integer} y
|
||||
-- @param {integer} r
|
||||
-- @param {integer} lineSize
|
||||
-- @param {color} drawColor
|
||||
----------------------------------------
|
||||
on circle (img, x, y, r, lineSize, drawColor)
|
||||
props = [:]
|
||||
props[#shapeType] = #oval
|
||||
props[#lineSize] = lineSize
|
||||
props[#color] = drawColor
|
||||
img.draw(x-r, y-r, x+r, y+r, props)
|
||||
end
|
||||
31
Task/Draw-a-sphere/Nim/draw-a-sphere.nim
Normal file
31
Task/Draw-a-sphere/Nim/draw-a-sphere.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import math
|
||||
|
||||
type Point = tuple[x,y,z: float]
|
||||
|
||||
const shades = ".:!*oe&#%@"
|
||||
|
||||
proc normalize(x, y, z: float): Point =
|
||||
let len = sqrt(x*x + y*y + z*z)
|
||||
(x / len, y / len, z / len)
|
||||
|
||||
proc dot(a, b: Point): float =
|
||||
result = max(0, - a.x*b.x - a.y*b.y - a.z*b.z)
|
||||
|
||||
let light = normalize(30.0, 30.0, -50.0)
|
||||
|
||||
proc drawSphere(r, k, ambient) =
|
||||
for i in -r .. r:
|
||||
let x = i.float + 0.5
|
||||
for j in -2*r .. 2*r:
|
||||
let y = j.float / 2.0 + 0.5
|
||||
if x*x + y*y <= float r*r:
|
||||
let
|
||||
v = normalize(x, y, sqrt(float(r*r) - x*x - y*y))
|
||||
b = pow(dot(light, v), k) + ambient
|
||||
i = clamp(int((1.0 - b) * shades.high.float), 0, shades.high)
|
||||
stdout.write shades[i]
|
||||
else: stdout.write ' '
|
||||
stdout.write "\n"
|
||||
|
||||
drawSphere 20, 4.0, 0.1
|
||||
drawSphere 10, 2.0, 0.4
|
||||
33
Task/Draw-a-sphere/Sidef/draw-a-sphere.sidef
Normal file
33
Task/Draw-a-sphere/Sidef/draw-a-sphere.sidef
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
func normalize (vec) { vec »/» (vec »*« vec -> sum.sqrt) }
|
||||
func dot (x, y) { -(x »*« y -> sum) ^max^ 0 }
|
||||
|
||||
var x = var y = 255
|
||||
x += 1 if x.is_even # must be odd
|
||||
|
||||
var light = normalize([ 3, 2, -5 ])
|
||||
var depth = 255
|
||||
|
||||
func draw_sphere(rad, k, ambient) {
|
||||
var pixels = []
|
||||
var r2 = (rad * rad)
|
||||
var range = (-rad .. rad)
|
||||
for x,y in (range ~X range) {
|
||||
if ((var x2 = x*x) + (var y2 = y*y) < r2) {
|
||||
var vector = normalize([x, y, (r2 - x2 - y2).sqrt])
|
||||
var intensity = (dot(light, vector)**k + ambient)
|
||||
var pixel = (0 ^max^ (intensity*depth -> int) ^min^ depth)
|
||||
pixels << pixel
|
||||
}
|
||||
else {
|
||||
pixels << 0
|
||||
}
|
||||
}
|
||||
return pixels
|
||||
}
|
||||
|
||||
var outfile = %f'sphere-sidef.pgm'
|
||||
var out = outfile.open('>:raw')
|
||||
|
||||
out.say("P5\n#{x} #{y}\n#{depth}") # .pgm header
|
||||
out.write(draw_sphere((x-1)/2, .9, .2).map{.chr}.join)
|
||||
out.close
|
||||
31
Task/Draw-a-sphere/Swift/draw-a-sphere.swift
Normal file
31
Task/Draw-a-sphere/Swift/draw-a-sphere.swift
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
class Sphere: UIView{
|
||||
|
||||
override func drawRect(rect: CGRect)
|
||||
{
|
||||
let context = UIGraphicsGetCurrentContext()
|
||||
let locations: [CGFloat] = [0.0, 1.0]
|
||||
|
||||
let colors = [UIColor.whiteColor().CGColor,
|
||||
UIColor.blueColor().CGColor]
|
||||
|
||||
let colorspace = CGColorSpaceCreateDeviceRGB()
|
||||
|
||||
let gradient = CGGradientCreateWithColors(colorspace,
|
||||
colors, locations)
|
||||
|
||||
var startPoint = CGPoint()
|
||||
var endPoint = CGPoint()
|
||||
startPoint.x = self.center.x - (self.frame.width * 0.1)
|
||||
startPoint.y = self.center.y - (self.frame.width * 0.15)
|
||||
endPoint.x = self.center.x
|
||||
endPoint.y = self.center.y
|
||||
let startRadius: CGFloat = 0
|
||||
let endRadius: CGFloat = self.frame.width * 0.38
|
||||
|
||||
CGContextDrawRadialGradient (context, gradient, startPoint,
|
||||
startRadius, endPoint, endRadius,
|
||||
0)
|
||||
}
|
||||
}
|
||||
|
||||
var s = Sphere(frame: CGRectMake(0, 0, 200, 200))
|
||||
19
Task/Draw-a-sphere/jq/draw-a-sphere-1.jq
Normal file
19
Task/Draw-a-sphere/jq/draw-a-sphere-1.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
def svg:
|
||||
"<svg width='100%' height='100%' version='1.1'
|
||||
xmlns='http://www.w3.org/2000/svg'
|
||||
xmlns:xlink='http://www.w3.org/1999/xlink'>" ;
|
||||
|
||||
# A radial gradient to make a circle look like a sphere.
|
||||
# "colors" should be [startColor, intermediateColor, endColor]
|
||||
# or null for ["white", "teal", "black"]
|
||||
def sphericalGradient(id; colors):
|
||||
"<defs>
|
||||
<radialGradient id = '\(id)' cx = '30%' cy = '30%' r = '100%' fx='10%' fy='10%' >
|
||||
<stop stop-color = '\(colors[0]//"white")' offset = '0%'/>
|
||||
<stop stop-color = '\(colors[1]//"teal")' offset = '50%'/>
|
||||
<stop stop-color = '\(colors[1]//"black")' offset = '100%'/>
|
||||
</radialGradient>
|
||||
</defs>" ;
|
||||
|
||||
def sphere(cx; cy; r; gradientId):
|
||||
"<circle fill='url(#\(gradientId))' cx='\(cx)' cy='\(cy)' r='\(r)' />" ;
|
||||
9
Task/Draw-a-sphere/jq/draw-a-sphere-2.jq
Normal file
9
Task/Draw-a-sphere/jq/draw-a-sphere-2.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def draw_sphere:
|
||||
svg,
|
||||
"<title>Teal sphere</title>",
|
||||
sphericalGradient("tealGradient"; null), # define the gradient to use
|
||||
sphere(100;100;100; "tealGradient"), # draw a sphere using the gradient
|
||||
sphere(100;300;100; "tealGradient"), # draw another sphere using the same gradient
|
||||
"</svg>" ;
|
||||
|
||||
draw_sphere
|
||||
1
Task/Draw-a-sphere/jq/draw-a-sphere-3.jq
Normal file
1
Task/Draw-a-sphere/jq/draw-a-sphere-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
$ jq -r -n -f spheres.jq > spheres.svg
|
||||
Loading…
Add table
Add a link
Reference in a new issue