public static Vector norm(Vector v) { var mag = Vector.mag(v); var div = (mag == 0) ? double.PositiveInfinity : 1.0 / mag; return(Vector.times(div, v)); }
Color addLight(Color col, Light light) { var ldis = Vector.minus(light.pos, pos); var livec = Vector.norm(ldis); var neatIsect = this.testRay(new Ray(pos, livec), scene); var isInShadow = (neatIsect == null) ? false : (neatIsect <= Vector.mag(ldis)); if (isInShadow) { return(col); } else { var illum = Vector.dot(livec, norm); var lcolor = (illum > 0) ? Color.scale(illum, light.color) : Color.defaultColor; var specular = Vector.dot(livec, Vector.norm(rd)); var scolor = (specular > 0) ? Color.scale(Math.Pow(specular, thing.surface.roughness), light.color) : Color.defaultColor; return(Color.plus(col, Color.plus(Color.times(thing.surface.diffuse(pos), lcolor), Color.times(thing.surface.specular(pos), scolor)))); } }