Given the ASCII art of two vectors, find the resultant vector's magnitude and degree.
Input
This can be received via STDIN, read from a local file, or provided through a function call. Here is an example of a two vector input:
^------>
|
|
|
x
This represents a change of 4 units north and 7 units east. Every input's starting point will be represented by an x
(decimal 120
).
All vectors are horizontal or vertical lines.
Each vector has one of these four endpoints:
^v<>
, and is made up of either a dash (-
, decimal 45) or a vertical bar (|
, decimal 124).Empty points on the plane are filled with spaces (
, decimal 32).
The input may be a single
x
.Adjacent vectors are always perpendicular to each other.
All vectors are tip-to-tail.
Output
This will be the displacement of the resulting point (distance from the starting point) and the degree to which it has moved, relative to the starting point.
For the above input, the output should be 8.06
units and 60.3
degrees. Each should have exactly 3 significant figures. Here are a few examples of numbers with 3 significant digits:
- 1.00
- 60.1
- 453
- 7.08
- 4.50
- 349
All unit measurements will be <= 999
.
These numbers should be output in the below format. This is using the numbers from above.
8.06 units @ 60.3 degrees
This may be followed by a single trailing space or newline.
If the input is a single x
, with no displacement and hence no angle of displacement, the output should be either an empty line (a single newline character) or in the following format:
0 units @ - degrees
If you're trying to qualify for the bonus, the direction should be -
as well.
In the case that bonuses 2, 3, or both are completed, the output should follow the below model and abide by the same restrictions as the above.
8.06 units @ 60.3 degrees NE
Degrees should be measured according to the standard plane.
90
135 | 45
\|/
180 ---x---- 0
/|\
225 | 315
270
0
degrees is east, 1 - 89
degrees is northeast, 90
is north, etc.
Bonuses
The following are worth a total of -50%.
Take a -10% bonus for each additional vector that can be handled. This bonus can be applied to up to 3 times. Vectors will never overlap or cross.
Take a -10% bonus if your output includes the cardinal direction of the angle (north, south, east, west).
Take a -10% bonus if your output includes the intermediate directions of the angle (northeast, northwest, southeast, southwest).
Examples
In:
x---->
|
v
Out:
5.39 units @ 338 degrees
Optionally SE
In:
<--------------^
|
|
x
Out:
15.3 units @ 169 degrees
Optionally NW
In:
x
|
|<-----^
| |
v------>
Out:
2.24 units @ 297 degrees
Optionally SE
Examples (multiple vectors)
In:
x--->
|
|
v----------->
Out:
16.3 units @ 349 degrees
Optionally SE
In:
<-------^
| |
| |
v |
|
|
x
Out:
8.54 units @ 159 degrees
Optionally NW
In:
^-->
| |
| v
|
<--------x
Out:
6.32 units @ 162 degrees
Optionally NW
x
? What's the boundary between North and Northwest? \$\endgroup\$x-->
. Can vectors cross? \$\endgroup\$x
. There may be more than two (if attempting to complete the bonus), but not less. I'm working on examples for multiple vector inputs. In no inputs will vectors cross. @ThomasKwa \$\endgroup\$