How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the Commodore Basic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the Commodore Basic programming language

Table of Contents

Problem Statement

Using the data storage type defined on this page for raster images, and the draw_line function defined in this one, draw a quadratic bezier curve (definition on Wikipedia).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the Commodore Basic programming language

Source code in the commodore programming language

   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 dim pt%(ns,2) : rem individual lines in curve
  130 gosub 3000
  140 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
 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 for i=0 to ns
 3020 t=i/ns
 3030 t1=1.0-t
 3040 a=t1^2
 3050 b=2.0*t*t1
 3060 c=t^2
 3070 pt(i,0)=a*bp(0,0)+b*bp(1,0)+c*bp(2,0)
 3080 pt(i,1)=a*bp(0,1)+b*bp(1,1)+c*bp(2,1)
 3090 next i
 3100 for i=0 to ns-1
 3110 x0=int(pt(i,0))
 3120 y0=int(pt(i,1))
 3130 x1=int(pt(i+1,0))
 3140 y1=int(pt(i+1,1))
 3150 gosub 1000
 3160 next i
 3170 return


  

You may also check:How to resolve the algorithm Operator precedence step by step in the J programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the Frink programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Swift programming language
You may also check:How to resolve the algorithm Function definition step by step in the Hy programming language