simonech / ray-tracer-challenge-netcore Goto Github PK
View Code? Open in Web Editor NEWMy attempt at implementing the The Ray Tracer Challenge book in .NET Core and C#
License: MIT License
My attempt at implementing the The Ray Tracer Challenge book in .NET Core and C#
License: MIT License
Hey,
With your Projectile's velocity, I believe it should be normalised "Tuple.Vector(1, 1, 0).Normalise()"
P.S Thanks for kicking off this - I've got the book and I'm following along!
The way the code is written might be your preferences and therefore I did not submit a pull request.
codeclimber.raytracer
use CodeClimber.RayTracer
this.
and other parts of code not using this.
, particularly in Tuple.csIEquatable<T>
and also implement override bool Equals(object obj)
with override int GetHashCode()
. This is new for me, so the idea of the suggestion is sound but the implementation might be questionable. At first I was wondering why even have this and the reason is to assume equality for doubles with a particular precision, 4.000001 equals 4.000002 are equal. Performance it part of this hence the duplication of the testing for equality. In examples I have see the equality test is in both override bool Equals(object obj)
and bool Equals(T other)
instead of having override bool Equals(object obj)
call bool Equals(T other)
after unboxing. But what is a few milliseconds. See below for implementation.Tuple(4.3, -4.000001, 3.1, 0)
and Tuple(4.3, -4.000002, 3.1, 0)
or testing the extension method.public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode();
}
public override bool Equals(object obj)
{
Tuple other = obj as Tuple;
if (other == null)
{
return false;
}
return (other.X.EqualsD(X)
&& other.Y.EqualsD(Y)
&& other.Z.EqualsD(Z)
&& other.W.EqualsD(W));
}
public bool Equals(Tuple other)
{
if (other == null)
{
return false;
}
return (other.X.EqualsD(X)
&& other.Y.EqualsD(Y)
&& other.Z.EqualsD(Z)
&& other.W.EqualsD(W));
}
I got the book!
In one of the first post you reason about concrete classes for vector and point. I think I have some suggestions. You can review my code referenced above.
After getting into Chapter 6 and having Tuple types throughout the code and not knowing if the property or variable is a Point or a Vector I decided to look into making concrete types.
I was able to build the concrete types Tuple, Vector, and Point, it appears to be a lot of duplicate code, but the code that is duplicated are "one liners" of code. Mainly the operator methods, returning their respective type after doing an operation instead of Tuple with the W set to 0 or 1 to designate a vector or point.
I had built four classes, a RtBaseTuple
which the other three are derived from, RtTuple
, RtVector
, and RtPoint
. The base class has all the properties along with the operations, Dot, Magnitude, that return the same type regardless of the derived type. The derived classes have methods in them that will return their specific type after doing *, /, +, Normalized, Cross, etc.
I believe the author did not want to complicate things with multiple things. Also by way of using some magic was able to have a Vector created from a Point by doing math. I removed some of the magic by implying the rules instead of having them inferred. If you subtract a point from a point return a vector instead of tuple that is a vector because the W went to 0 after subtracting 1 from 1.
In my RtPoint
class I have this, subtract two points and return a vector.
static public RtVector operator -(RtPoint left, RtPoint right)
=> new RtVector(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
I think the biggest challenge and reason for duplicate code for a derived type is not being able to return a different type. There is no way to have a class Tuple
with a virtual Negate
or Normalized
that you can override with a different return type . In the derived class Vector
you can't override Normalized and return Vector
, it is expecting Tuple
to be returned. Something about co-variance. But even if you did have something like this you would still end up with duplicate code or code wrapping other code.
I think the thing that hit me was the author eluding to having Color be like a tuple and reuse the code, but there was no way of doing that and if you did you still end up with duplicate code trying to wrap X around Red, Y around Green and Z around Blue, and ignoring the W property. Hence why there is a class just for color. Also in part of the book there is math that breaks the magic, after the operation is done you have to reset the W value.
This hacking was a testament to having a boat load of tests. After screwing around and getting everything to pass the test, my exercises still worked as they should.
System.Numerics provides Vector3 and Vector4 structures, as well as Matrixes, and all the possible operations.
Those classes are also optimized to use SIMD operations in the CPU.
After the actual exercise of the book is complete, it might be a good idea to try and replace the primitives implemented from scratch in chapters 1-4 with these purpose mades classes
Hi @simonech
Good to see someone else doing the ray tracer challenge with .NET CORE!
I have some suggestions if you don't mind :)
public static Tupple operator +(Tupple left, Tupple right)
{
return new Tupple(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
usage:
var t1 = new Tupple(1.0f, 1.0f, 1.0f, 1.0f);
var t2 = new Tupple(2.0f, 2.0f, 2.0f, 2.0f);
var sum = t1 + t2;
2.1 preview
it has included a math library for float valuesA declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.