Use infix dot product function.

This commit is contained in:
Sven Weidauer 2024-12-01 12:22:37 +01:00
parent 18aefdf41d
commit 20e50ad7b9
5 changed files with 6 additions and 8 deletions

View file

@ -1,8 +1,8 @@
data class Plane(val point: Point, val normal: Vector, val material: Material) : Thing {
override fun intersects(ray: Ray): Hit? {
val bottom = dot(normal, ray.direction)
val bottom = normal dot ray.direction
if (bottom == 0.0f) return null
val t = dot(normal, point - ray.origin) / bottom
val t = (normal dot point - ray.origin) / bottom
if (t <= 0.0f) return null
return Hit(ray.at(t), normal, t, material)
}

View file

@ -7,8 +7,6 @@ data class Ray(val origin: Point, val direction: Vector) {
}
fun dot(lhs: Vector, rhs: Vector): Float = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z
fun main() {
val bmp = Bitmap(1000, 1000)
val origin = Point(500f, 500f, -500f)

View file

@ -13,7 +13,7 @@ data class ReflectiveMaterial(
val rayToLight = offsetPoint.rayTo(light.point)
if (scene.intersects(rayToLight) == null) {
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
val lambert = java.lang.Float.max(0f, hit.normal dot rayToLight.direction)
color += lambert * light.color
val h = (-ray.direction + rayToLight.direction).normalized()

View file

@ -4,7 +4,7 @@ import kotlin.math.sqrt
data class Sphere(val center: Point, val radius: Float, val material: Material) : Thing {
override fun intersects(ray: Ray): Hit? {
val l = center - ray.origin
val tc = dot(l, ray.direction)
val tc = l dot ray.direction
if (tc < 0f) return null
val d = l.squaredLength - tc * tc

View file

@ -10,12 +10,12 @@ data class WhateverMaterial(val color: MaterialColor,
val rayToLight = (hit.point + 0.0001f * hit.normal).rayTo(light.point)
if (scene.intersects(rayToLight) == null) {
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
val lambert = java.lang.Float.max(0f, hit.normal dot rayToLight.direction)
color += lambert * light.color
val h = (-ray.direction + rayToLight.direction).normalized()
val intensity = dot(hit.normal, h).pow(specular)
val intensity = (hit.normal dot h).pow(specular)
color += intensity * MaterialColor(0.3f, 0f, 0f)
}