[ prog / sol / mona ]

prog


GPT3 code

1 2022-12-07 02:17

GPT3 will just write code from prompts.
Here's a scheme raytracer it wrote. I doubt it works but I don't have scheme installed here:
;; Define a structure to represent a 3D point
(define-struct point (x y z))

;; Define a structure to represent a 3D vector
(define-struct vector (x y z))

;; Define a structure to represent a ray
(define-struct ray (origin direction))

;; Define a structure to represent a sphere
(define-struct sphere (center radius))

;; Function to add two vectors
(define (vector-add v1 v2)
(make-vector (+ (vector-x v1) (vector-x v2))
(+ (vector-y v1) (vector-y v2))
(+ (vector-z v1) (vector-z v2))))

;; Function to subtract two vectors
(define (vector-sub v1 v2)
(make-vector (- (vector-x v1) (vector-x v2))
(- (vector-y v1) (vector-y v2))
(- (vector-z v1) (vector-z v2))))

;; Function to multiply a vector by a scalar
(define (vector-mul v c)
(make-vector (* (vector-x v) c)
(* (vector-y v) c)
(* (vector-z v) c)))

;; Function to dot product of two vectors
(define (dot v1 v2)
(+ (* (vector-x v1) (vector-x v2))
(* (vector-y v1) (vector-y v2))
(* (vector-z v1) (vector-z v2))))

;; Function to cross product of two vectors
(define (cross v1 v2)
(make-vector (- (* (vector-y v1) (vector-z v2)) (* (vector-z v1) (vector-y v2)))
(- (* (vector-z v1) (vector-x v2)) (* (vector-x v1) (vector-z v2)))
(- (* (vector-x v1) (vector-y v2)) (* (vector-y v1) (vector-x v2)))))

;; Function to compute the normal vector of a sphere
(define (sphere-normal sphere point)
(vector-normalize (vector-sub point (sphere-center sphere))))

;; Function to compute the intersection of a ray and a sphere
(define (intersect-sphere ray sphere)
(let* ((origin (ray-origin ray))
(direction (ray-direction ray))
(center (sphere-center sphere))
(radius (sphere-radius sphere))
(oc (vector-sub origin center))
(a (dot direction direction))
(b (dot oc direction))
(c (dot oc oc) - (* radius radius)))
(let ((discriminant (sqrt (- (* b b) (* a c)))))
(if (< discriminant 0)
#f
(let ((t1 (/ (- b) a))
(t2 (/ (+ b) a)))
(let ((t (if (< t1 0) t2 t1)))
(if (< t 0)
#f
(vector-add origin (vector-mul direction t)))))))))
vector-mul direction t)))))))))

;; Function to compute the color of a sphere
(define (sphere-color sphere point)
(let ((normal (sphere-normal sphere point)))
(let ((r (abs (vector-x normal)))
(g (abs (vector-y normal)))
(b (abs (vector-z normal))))
(list r g b))))

;; Function to render an image
(define (render width height spheres)
(let ((image (make-vector width height)))
(let loop ((y 0) (x 0))
(if (< y height)
(if (< x width)
(let* ((origin (make-point 0 0 0))
(direction (make-vector x y 1))
(ray (make-ray origin direction))
(color (let loop ((spheres spheres))
(if (null? spheres)
(list 0 0 0)
(let ((intersection (intersect-sphere ray (car spheres))))
(if intersection
(sphere-color (car spheres) intersection)
(loop (cdr spheres)))))))
(r (floor (* (car color) 255)))
(g (floor (* (cadr color) 255)))
(b (floor (* (caddr color) 255))))
(vector-set! image y (vector-set! (vector-ref image y) x (list r g b)))
(loop y (+ x 1)))
(loop (+ y 1) 0)))
image))))

;; Example usage
(let ((spheres (list (make-sphere (make-point 0 0 10) 1)
(make-sphere (make-point 0 0 20) 1))))
(let ((image (render 800 600 spheres)))
(for-each (lambda (row)
(for-each (lambda (pixel)
(display (car pixel))
(display (cadr pixel))
(display (caddr pixel)))
row)
(newline))
image)))

23


VIP:

do not edit these