How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Racket 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 other one, draw a cubic 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/Cubic step by step in the Racket programming language

Source code in the racket programming language

#lang racket
(require racket/draw)

(define (draw-line dc p q)
  (match* (p q) [((list x y) (list s t)) (send dc draw-line x y s t)]))

(define (draw-lines dc ps)
  (void
   (for/fold ([p0 (first ps)]) ([p (rest ps)])
     (draw-line dc p0 p)
     p)))

(define (int t p q)
  (define ((int1 t) x0 x1) (+ (* (- 1 t) x0) (* t x1)))
  (map (int1 t) p q))
  
(define (bezier-points p0 p1 p2 p3)
  (for/list ([t (in-range 0.0 1.0 (/ 1.0 20))])
    (int t (int t p0 p1) (int t p2 p3))))

(define bm (make-object bitmap% 17 17))
(define dc (new bitmap-dc% [bitmap bm]))
(send dc set-smoothing 'unsmoothed)
(send dc set-pen "red" 1 'solid)
(draw-lines dc (bezier-points '(16 1) '(1 4) '(3 16) '(15 11)))
bm


  

You may also check:How to resolve the algorithm Sierpinski arrowhead curve step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Anadromes step by step in the Ada programming language
You may also check:How to resolve the algorithm A+B step by step in the bc programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the zkl programming language