How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the Action! 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 Action! 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 Action! programming language

Source code in the action! programming language

INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
INCLUDE "H6:REALMATH.ACT"

RGB black,yellow,violet,blue

TYPE IntPoint=[INT x,y]

PROC QuadraticBezier(RgbImage POINTER img
  IntPoint POINTER p1,p2,p3 RGB POINTER col)
  INT i,n=[20],prevX,prevY,nextX,nextY
  REAL one,two,ri,rn,rt,ra,rb,rc,tmp1,tmp2,tmp3
  REAL x1,y1,x2,y2,x3,y3

  IntToReal(p1.x,x1) IntToReal(p1.y,y1)
  IntToReal(p2.x,x2) IntToReal(p2.y,y2)
  IntToReal(p3.x,x3) IntToReal(p3.y,y3)
  IntToReal(1,one)   IntToReal(2,two)
  IntToReal(n,rn)
  FOR i=0 TO n
  DO
    prevX=nextX prevY=nextY

    IntToReal(i,ri)
    RealDiv(ri,rn,rt)       ;t=i/n
    
    RealSub(one,rt,tmp1)    ;tmp1=1-t
    RealMult(tmp1,tmp1,ra)  ;a=(1-t)^2
    
    RealMult(two,rt,tmp2)   ;tmp2=2*t
    RealMult(tmp2,tmp1,rb)  ;b=2*t*(1-t)

    RealMult(rt,rt,rc)      ;c=t^2

    RealMult(ra,x1,tmp1)    ;tmp1=a*x1
    RealMult(rb,x2,tmp2)    ;tmp2=b*x2
    RealAdd(tmp1,tmp2,tmp3) ;tmp3=a*x1+b*x2
    RealMult(rc,x3,tmp1)    ;tmp1=c*x3
    RealAdd(tmp3,tmp1,tmp2) ;tmp2=a*x1+b*x2+c*x3
    nextX=Round(tmp2)

    RealMult(ra,y1,tmp1)    ;tmp1=a*y1
    RealMult(rb,y2,tmp2)    ;tmp2=b*y2
    RealAdd(tmp1,tmp2,tmp3) ;tmp3=a*y1+b*y2
    RealMult(rc,y3,tmp1)    ;tmp1=c*y3
    RealAdd(tmp3,tmp1,tmp2) ;tmp2=a*y1+b*y2+c*y3
    nextY=Round(tmp2)

    IF i>0 THEN
      RgbLine(img,prevX,prevY,nextX,nextY,col)
    FI
  OD
RETURN

PROC DrawImage(RgbImage POINTER img BYTE x,y)
  RGB POINTER ptr
  BYTE i,j

  ptr=img.data
  FOR j=0 TO img.h-1
  DO
    FOR i=0 TO img.w-1
    DO
      IF RgbEqual(ptr,yellow) THEN
        Color=1
      ELSEIF RgbEqual(ptr,violet) THEN
        Color=2
      ELSEIF RgbEqual(ptr,blue) THEN
        Color=3
      ELSE
        Color=0
      FI
      Plot(x+i,y+j)
      ptr==+RGBSIZE
    OD
  OD  
RETURN

PROC Main()
  RgbImage img
  BYTE CH=$02FC,width=[70],height=[40]
  BYTE ARRAY ptr(8400)
  IntPoint p1,p2,p3

  Graphics(7+16)
  SetColor(0,13,12) ;yellow
  SetColor(1,4,8)   ;violet
  SetColor(2,8,6)   ;blue
  SetColor(4,0,0)   ;black

  RgbBlack(black)
  RgbYellow(yellow)
  RgbViolet(violet)
  RgbBlue(blue)

  InitRgbImage(img,width,height,ptr)
  FillRgbImage(img,black)

  p1.x=0  p1.y=3
  p2.x=47 p2.y=39
  p3.x=69 p3.y=12
  RgbLine(img,p1.x,p1.y,p2.x,p2.y,blue)
  RgbLine(img,p2.x,p2.y,p3.x,p3.y,blue)
  QuadraticBezier(img,p1,p2,p3,yellow)
  SetRgbPixel(img,p1.x,p1.y,violet)
  SetRgbPixel(img,p2.x,p2.y,violet)
  SetRgbPixel(img,p3.x,p3.y,violet)

  DrawImage(img,(160-width)/2,(96-height)/2)

  DO UNTIL CH#$FF OD
  CH=$FF
RETURN

  

You may also check:How to resolve the algorithm Queue/Definition step by step in the PL/I programming language
You may also check:How to resolve the algorithm Number names step by step in the Pascal programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the J programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Racket programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Delphi programming language