Bitmap/Bézier curves/Cubic: Difference between revisions

Added Commodore BASIC solution
(Added Kotlin)
(Added Commodore BASIC solution)
Line 241:
#undef plot
#undef line</lang>
 
=={{header|Commodore Basic}}==
<lang basic>
10 rem bezier curve algorihm
20 rem translated from purebasic
30 ns=25 : rem num segments
40 dim pt(ns,2) : rem points in line
50 sc=1024 : rem start of screen memory
60 sw=40 : rem screen width
70 sh=25 : rem screen height
80 pc=42 : rem plot character '*'
90 dim bp(2,1) : rem bezier curve points
100 bp(0,0)=1:bp(1,0)=70:bp(2,0)=1
110 bp(0,1)=1:bp(1,1)=8:bp(2,1)=23
120 gosub 3000
130 end
1000 rem plot line
1010 se=0 : rem 0 = steep 1 = !steep
1020 if abs(y1-y0)>abs(x1-x0) then se=1:tp=y0:y0=x0:x0=tp:tp=y1:y1=x1:x1=tp
1030 if x0>x1 then tp=x1:x1=x0:x0=tp:tp=y1:y1=y0:y0=tp
1040 dx=x1-x0
1050 dy=abs(y1-y0)
1060 er=dx/2
1070 y=y0
1080 ys=-1
1090 if y0<y1 then ys = 1
1100 for x=x0 to x1
1110 if se=1 then p0=y: p1=x:gosub 2000:goto 1130
1120 p0=x: p1=y: gosub 2000
1130 er=er-dy
1140 if er<0 then y=y+ys:er=er+dx
1150 next x
1160 return
2000 rem plot individual point
2010 rem p0 == plot point x
2020 rem p1 == plot point y
2030 sl=p0+(p1*sw)
2040 rem make sure we dont write beyond screen memory
2050 if sl<(sw*sh) then poke sc+sl,pc
2060 return
3000 rem bezier curve
3010 dim pt%(ns,2)
3020 for i=0 to ns
3030 t=i/ns
3040 t1=1.0-t
3050 a=t1^2
3060 b=2.0*t*t1
3070 c=t^2
3080 pt(i,0)=a*bp(0,0)+b*bp(1,0)+c*bp(2,0)
3090 pt(i,1)=a*bp(0,1)+b*bp(1,1)+c*bp(2,1)
3100 next i
3110 for i=0 to ns-1
3120 x0=int(pt(i,0))
3130 y0=int(pt(i,1))
3140 x1=int(pt(i+1,0))
3150 y1=int(pt(i+1,1))
3160 gosub 1000
3170 next i
3180 return
</lang>
[https://www.worldofchris.com/assets/c64-bezier-curve.png Screenshot of Bézier curve on C64]
 
 
=={{header|D}}==