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
|
|
@ -0,0 +1,30 @@
|
|||
PROGRAM BRESENHAM
|
||||
|
||||
!$INCLUDE="PC.LIB"
|
||||
|
||||
PROCEDURE BRESENHAM
|
||||
! === Draw a line using graphic coordinates
|
||||
! Inputs are X1, Y1, X2, Y2: Destroys value of X1, Y1
|
||||
dx=ABS(x2-x1) sx=-1
|
||||
IF x1<x2 THEN sx=1
|
||||
dy=ABS(y2-y1) sy=-1
|
||||
IF y1<y2 THEN sy=1
|
||||
er=-dy
|
||||
IF dx>dy THEN er=dx
|
||||
er=INT(er/2)
|
||||
LOOP
|
||||
PSET(x1,y1,1)
|
||||
EXIT IF x1=x2 AND y1=y2
|
||||
e2=er
|
||||
IF e2>-dx THEN er=er-dy x1=x1+sx
|
||||
IF e2<dy THEN er=er+dx y1=y1+sy
|
||||
END LOOP
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
SCREEN(2)
|
||||
INPUT(x1,y1,x2,y2)
|
||||
BRESENHAM
|
||||
GET(A$)
|
||||
SCREEN(0)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
' version 16-09-2015
|
||||
' compile with: fbc -s console
|
||||
' OR compile with: fbc -s gui
|
||||
|
||||
' Ported from the C version
|
||||
Sub Br_line(x0 As Integer, y0 As Integer, x1 As Integer, y1 As Integer, Col As Integer = &HFFFFFF)
|
||||
|
||||
Dim As Integer dx = Abs(x1 - x0), dy = Abs(y1 - y0)
|
||||
Dim As Integer sx = IIf(x0 < x1, 1, -1)
|
||||
Dim As Integer sy = IIf(y0 < y1, 1, -1)
|
||||
Dim As Integer er = IIf(dx > dy, dx, -dy) \ 2, e2
|
||||
|
||||
Do
|
||||
PSet(x0, y0), col
|
||||
If (x0 = x1) And (y0 = y1) Then Exit Do
|
||||
e2 = er
|
||||
If e2 > -dx Then Er -= dy : x0 += sx
|
||||
If e2 < dy Then Er += dx : y0 += sy
|
||||
Loop
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Double x0, y0, x1, y1
|
||||
|
||||
ScreenRes 400, 400, 32
|
||||
WindowTitle" Press key to end demo"
|
||||
Randomize Timer
|
||||
|
||||
Do
|
||||
Cls
|
||||
For a As Integer = 1 To 20
|
||||
Br_line(Rnd*380+10, Rnd*380+10, Rnd*380+10, Rnd*380+10, Rnd*&hFFFFFF)
|
||||
Next
|
||||
Sleep 2000
|
||||
Loop Until InKey <> "" ' loop until a key is pressed
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
import math
|
||||
|
||||
proc line(img: var Image, p, q: Point) =
|
||||
let
|
||||
dx = abs(q.x - p.x)
|
||||
sx = if p.x < q.x: 1 else: -1
|
||||
dy = abs(q.y - p.y)
|
||||
sy = if p.y < q.y: 1 else: -1
|
||||
|
||||
var
|
||||
p = p
|
||||
q = q
|
||||
err = (if dx > dy: dx else: -dy) div 2
|
||||
e2 = 0
|
||||
|
||||
while true:
|
||||
img[p] = Black
|
||||
if p == q:
|
||||
break
|
||||
e2 = err
|
||||
if e2 > -dx:
|
||||
err -= dy
|
||||
p.x += sx
|
||||
if e2 < dy:
|
||||
err += dx
|
||||
p.y += sy
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
function bresLine(sequence screenData, integer x0, integer y0, integer x1, integer y1, integer colour)
|
||||
-- The line algorithm
|
||||
integer deltaX = abs(x1-x0),
|
||||
deltaY = abs(y1-y0),
|
||||
stepX = iff(x0<x1,1,-1),
|
||||
stepY = iff(y0<y1,1,-1),
|
||||
lineError = iff(deltaX>deltaY,deltaX,-deltaY),
|
||||
prevle
|
||||
|
||||
lineError = round(lineError/2,1)
|
||||
while 1 do
|
||||
if x0>=1 and x0<=length(screenData)
|
||||
and y0>=1 and y0<=length(screenData[x0]) then
|
||||
screenData[x0][y0] = colour
|
||||
end if
|
||||
if x0=x1 and y0=y1 then exit end if
|
||||
prevle = lineError
|
||||
if prevle>-deltaX then
|
||||
lineError -= deltaY
|
||||
x0 += stepX
|
||||
end if
|
||||
if prevle<deltaY then
|
||||
lineError += deltaX
|
||||
y0 += stepY
|
||||
end if
|
||||
end while
|
||||
return screenData
|
||||
end function
|
||||
|
||||
sequence screenData = new_image(400,300,black)
|
||||
screenData = bresLine(screenData,100,1,50,300,red)
|
||||
screenData = bresLine(screenData,1,180,400,240,green)
|
||||
screenData = bresLine(screenData,200,1,400,150,white)
|
||||
screenData = bresLine(screenData,195,1,205,300,blue)
|
||||
write_ppm("bresenham.ppm",screenData)
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
load "guilib.ring"
|
||||
load "stdlib.ring"
|
||||
|
||||
new qapp
|
||||
{
|
||||
win1 = new qwidget() {
|
||||
setwindowtitle("drawing using qpainter")
|
||||
setwinicon(self,"c:\ring\bin\image\browser.png")
|
||||
setgeometry(100,100,500,600)
|
||||
label1 = new qlabel(win1) {
|
||||
setgeometry(10,10,400,400)
|
||||
settext("")
|
||||
}
|
||||
new qpushbutton(win1) {
|
||||
setgeometry(200,400,100,30)
|
||||
settext("draw")
|
||||
setclickevent("draw()")
|
||||
}
|
||||
show()
|
||||
}
|
||||
exec()
|
||||
}
|
||||
|
||||
func draw
|
||||
p1 = new qpicture()
|
||||
color = new qcolor() {
|
||||
setrgb(0,0,255,255)
|
||||
}
|
||||
pen = new qpen() {
|
||||
setcolor(color)
|
||||
setwidth(1)
|
||||
}
|
||||
new qpainter() {
|
||||
begin(p1)
|
||||
setpen(pen)
|
||||
|
||||
line = [[50,100,100,190], [100,190,150,100], [150,100,100,10], [100,10,50,100]]
|
||||
|
||||
for n = 1 to 4
|
||||
x1=line[n][1] y1=line[n][2] x2=line[n][3] y2=line[n][4]
|
||||
dx = fabs(x2 - x1) sx = sign(x2 - x1)
|
||||
dy = fabs(y2 - y1) sy = sign(y2 - y1)
|
||||
if dx < dy e = dx / 2 else e = dy / 2 ok
|
||||
while true
|
||||
drawline (x1*2,y1*2,x2*2,y2*2)
|
||||
if x1 = x2 if y1 = y2 exit ok ok
|
||||
if dx > dy
|
||||
x1 += sx e -= dy if e < 0 e += dx y1 += sy ok
|
||||
else
|
||||
y1 += sy e -= dx if e < 0 e += dy x1 += sx ok ok
|
||||
end
|
||||
next
|
||||
|
||||
endpaint()
|
||||
}
|
||||
label1 { setpicture(p1) show() }
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
func my_draw_line(img, x0, y0, x1, y1) {
|
||||
|
||||
var steep = (abs(y1 - y0) > abs(x1 - x0))
|
||||
|
||||
if (steep) {
|
||||
(y0, x0) = (x0, y0)
|
||||
(y1, x1) = (x1, y1)
|
||||
}
|
||||
if (x0 > x1) {
|
||||
(x1, x0) = (x0, x1)
|
||||
(y1, y0) = (y0, y1)
|
||||
}
|
||||
|
||||
var deltax = (x1 - x0)
|
||||
var deltay = abs(y1 - y0)
|
||||
var error = (deltax / 2)
|
||||
var y = y0
|
||||
var ystep = (y0 < y1 ? 1 : -1)
|
||||
|
||||
x0.to(x1).each { |x|
|
||||
img.draw_point(steep ? ((y, x)) : ((x, y)))
|
||||
error -= deltay
|
||||
if (error < 0) {
|
||||
y += ystep
|
||||
error += deltax
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require('Image::Imlib2')
|
||||
|
||||
var img = %s'Image::Imlib2'.new(160, 160)
|
||||
img.set_color(255, 255, 255, 255) # white
|
||||
img.fill_rectangle(0,0,160,160)
|
||||
|
||||
img.set_color(0,0,0,255) # black
|
||||
my_draw_line(img, 10, 80, 80, 160)
|
||||
my_draw_line(img, 80, 160, 160, 80)
|
||||
my_draw_line(img, 160, 80, 80, 10)
|
||||
my_draw_line(img, 80, 10, 10, 80)
|
||||
|
||||
img.save("test0.png");
|
||||
|
||||
# let's try the same using its internal algo
|
||||
img.set_color(255, 255, 255, 255) # white
|
||||
img.fill_rectangle(0,0,160,160)
|
||||
img.set_color(0,0,0,255) # black
|
||||
img.draw_line(10, 80, 80, 160)
|
||||
img.draw_line(80, 160, 160, 80)
|
||||
img.draw_line(160, 80, 80, 10)
|
||||
img.draw_line(80, 10, 10, 80)
|
||||
|
||||
img.save("test1.png")
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# doesn't handle vertical lines
|
||||
def (line x0 y0 x1 y1)
|
||||
let steep ((> abs) y1-y0 x1-x0)
|
||||
when steep
|
||||
swap! x0 y0
|
||||
swap! x1 y1
|
||||
when (x0 > x1)
|
||||
swap! x0 x1
|
||||
swap! y0 y1
|
||||
withs (deltax x1-x0
|
||||
deltay (abs y1-y0)
|
||||
error deltax/2
|
||||
ystep (if (y0 < y1) 1 -1)
|
||||
y y0)
|
||||
for x x0 (x <= x1) ++x
|
||||
if steep
|
||||
plot y x
|
||||
plot x y
|
||||
error -= deltay
|
||||
when (error < 0)
|
||||
y += ystep
|
||||
error += deltax
|
||||
Loading…
Add table
Add a link
Reference in a new issue