From 20e50ad7b9d37217d68b5250494bf836f075d522 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 1 Dec 2024 12:22:37 +0100 Subject: [PATCH] Use infix dot product function. --- src/main/kotlin/Plane.kt | 4 ++-- src/main/kotlin/Ray.kt | 2 -- src/main/kotlin/ReflectiveMaterial.kt | 2 +- src/main/kotlin/Sphere.kt | 2 +- src/main/kotlin/WhateverMaterial.kt | 4 ++-- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/Plane.kt b/src/main/kotlin/Plane.kt index 91ef3f7..97a91a6 100644 --- a/src/main/kotlin/Plane.kt +++ b/src/main/kotlin/Plane.kt @@ -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) } diff --git a/src/main/kotlin/Ray.kt b/src/main/kotlin/Ray.kt index c218d62..1f06a97 100644 --- a/src/main/kotlin/Ray.kt +++ b/src/main/kotlin/Ray.kt @@ -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) diff --git a/src/main/kotlin/ReflectiveMaterial.kt b/src/main/kotlin/ReflectiveMaterial.kt index d0cdf58..c9d41b1 100644 --- a/src/main/kotlin/ReflectiveMaterial.kt +++ b/src/main/kotlin/ReflectiveMaterial.kt @@ -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() diff --git a/src/main/kotlin/Sphere.kt b/src/main/kotlin/Sphere.kt index 58ab018..2978c82 100644 --- a/src/main/kotlin/Sphere.kt +++ b/src/main/kotlin/Sphere.kt @@ -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 diff --git a/src/main/kotlin/WhateverMaterial.kt b/src/main/kotlin/WhateverMaterial.kt index 1d8aeca..a59b0c6 100644 --- a/src/main/kotlin/WhateverMaterial.kt +++ b/src/main/kotlin/WhateverMaterial.kt @@ -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) }