Line, Normalization
Description:
There's a lot of interesting and useful things you can do with vectors, and when comparing two vectors you can do other, equally-interesting things. Your task is to write a Line object (or "class", for you fancy folks) that tackles one line of interestingness: vector normalization.
There's a few steps you need to take to calculate a normalized vector, and one in particular is pretty computationally "slow". With this object, we want to be able to calculate any single one of these steps, but also have the ability to change the vector parameters and have them re-calculated the next time we want a given value.
Below is a spec of what properties need to do what. Have fun!
NOTE: If you have any questions, find anything confusing, or have better links or explanations I could use, I'd be glad to hear them. Normalization is a tricky concept to explain to the layman... ;P
new Line()
Should throw an error, encouraging the user (or programmer) to use Line.new()
instead (see below).
Line.maxPoolSize
The maximum number of "destroyed" Line objects that should be kept in memory and reused when creating a "new" Line object.
It defaults to Infinity
, which means they should always be stored. If the value is changed to a number smaller than the number of "destroyed" objects, extra references should be cleared, to allow that memory to be freed.
Line.new(from, to)
Returns a new Line object (Line.new() instanceof Line === true
).
Optionally takes from
and to
vectors, each with x
and y
properties with numerical values. These values should be added to the from
and to
properties of the resulting object unchanged, and will be used to calculate the rest of the properties (see below).
If an instance has been destroyed but kept in memory (see .destroy()
below), one of these destroyed objects should be used and set up using the from
and to
arguments as per usual. This allows the coder to keep things efficient. Instead of creating a load of Line objects and them being garbage collected when they aren't expecting it, they can be kept in-memory and reused later on.
.from, .to
The .from
and .to
properties are read-only, meaning if you try to write to them (change their value), nothing happens. Each has two properties of its own which are not read-only: .x
and .y
. Their values default to 0.
NOTE: All other values on the object are read-only.
Shorthands: There are shorthand properties on the base Line
object for these values. The properties .fromX
, .fromY
, .toX
, and .toY
are the same as referencing or setting .from.x
, .from.y
, .to.x
, and .to.y
respectively.
.difference (.diff)
The .difference
property has two properties of its own: .x
and .y
. These are the difference between .from.x
and .to.x
, and .from.y
and .to.y
respecively. So if .from.x = 10
and .to.x = -5
, .difference.x == -15
.
Shorthands: .diff
is linked to .difference
, .differenceX
and .diffX
are linked to .difference.x
, and .differenceY
and .diffY
are linked to .difference.y
.
.distance (.d)
The .distance
property returns the distance between the .from
and .to
vectors.
Pythagorean Theorem: Khan Academy, Wikipedia.
Shorthands: .d
is linked to .distance
.
.distanceSquared (.dSq)
The .distanceSquared
property returns the distance between the .from
and .to
vectors, squared.
Shorthands: .dSq
is linked to .distanceSquared
.
.normalized (.norm)
The .normalized
property has two properties of its own: .x
and .y
. These values are calulated so that the distance between (0, 0) and (.x
, .y
) is 1, and it would point in the same direction as .from
to .to
. Say if you were standing at point .from
, and looking at .to
. The .normalized
vector would be 1 "distance" directly in front of you. Hopefully the links will be clearer, if you are unfamiliar with the concept.
Normalizing Vectors: Khan Academy, Fundza.
Shorthands: .norm
is linked to .normalized
, .normalizedX
and .normX
are linked to .normalized.x
, and .normalizedY
and .normY
are linked to .normalized.y
.
.destroy()
If there are fewer destroyed Line objects in memory than the set Line.maxPoolSize
, the Line object should be stored in memory.
Otherwise, its values should be set to null and all references held by the Line code should be cleared. This will allow Garbage Collection to remove it from memory later on.
NOTE: Math.sqrt, Math.pow, and Math.hypot will be disabled until the last moment at which it is needed to calculate a value. Using those methods at any other time will throw an error.
Similar Kata:
Stats:
Created | Mar 20, 2015 |
Published | Mar 22, 2015 |
Warriors Trained | 61 |
Total Skips | 15 |
Total Code Submissions | 437 |
Total Times Completed | 8 |
JavaScript Completions | 8 |
Total Stars | 3 |
% of votes with a positive feedback rating | 100% of 4 |
Total "Very Satisfied" Votes | 4 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 4 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 7 kyu |