20
2
Help! My math exam is coming up soon and I didn't study! 1 Part of the exam is to classify a quadrilateral given its vertex coordinates, which I, unfortunately, do not know how to do. 2
So, your challenge is to write a program to do this for me so I don't fail!
Challenge
Given four vertices such that no three of them are colinear, determine the most specific classification of the quadrilateral formed by those four vertices.
What I mean by "most specific classification" is that even though all squares are rectangles, if the shape is a square, you should indicate that it is a square and not indicate that it is a rectangle.
Input
Input will be given as four (x, y) coordinates. You may take these as a list of length 4 of lists/tuples of length 2. Alternatively, you can take input as a list of the x-coordinates and a list of the respective y-coordinates.
For example, if my shape has vertices at points (0, 0)
, (5, 0)
, (6, 1)
, and (1, 1)
, you may choose to take input in either of the following formats or something similar:
[(0, 0), (5, 0), (6, 1), (1, 1)]
([0, 5, 6, 1], [0, 0, 1, 1])
You may assume that the quadrilateral is not self-intersecting and that the points are given in the correct order (that is, two consecutive points in the input will be connected by a line segment in the quadrilateral).
Output
You will need a unique output for each of the following classes of quadrilaterals:
- Square
- Rectangle
- Rhombus
- Parallelogram
- Trapezoid/Trapezium
- Kite
- Quadrilateral
This could be the exact name itself, a character, an integer, etc.
Rules
- Standard Loopholes Apply
- If your programming language has a built-in that will perform this exact task, that built-in is not allowed.
- Built-ins for finding the distance between two points are allowed.
- Built-ins for finding the angle between two lines are allowed.
At this point, if you know all of the terms, you are ready to start programming! (Test Cases are at the end)
Terminology
This section is for anyone who needs clarification on the definitions of the different shapes.
Square
A quadrilateral is a square if and only if all 4 of its sides are equal in length and every pair of adjacent sides is perpendicular (that is, it is both a rectangle and a rhombus).
Rectangle
A quadrilateral is a rectangle if and only if every pair of adjacent sides is perpendicular.
Rhombus
A quadrilateral is a rhombus if and only if all 4 of its sides are equal.
Parallelogram
A quadrilateral is a parallelogram if and only if each pair of opposite sides is parallel and each pair of opposite angles is equal. Both of these conditions imply each other so you only need to check for one of them.
Trapezoid/Trapezium
A quadrilateral is a trapezoid/trapezium if and only if it has at least one pair of parallel sides.
Kite
A quadrilateral is a kite if two opposite pairs of adjacent sides are equal in length; that is, two of its adjacent sides are equal and the other two are also equal.
Test Cases
input as (x, y) * 4 -> full name
[(0, 0), (1, 0), (1, 1), (0, 1)] -> square
[(0, 0), (1, 1), (-1, 3), (-2, 2)] -> rectangle
[(0, 0), (5, 0), (8, 4), (3, 4)] -> rhombus
[(0, 0), (5, 0), (6, 1), (1, 1)] -> parallelogram
[(0, 0), (4, 0), (3, 1), (1, 1)] -> trapezoid/trapezium
[(0, 0), (1, 1), (0, 3), (-1, 1)] -> kite
[(0, 0), (2, 0), (4, 4), (0, 1)] -> quadrilateral
Links (Desmos Graphing Calculator)
Here are links to visualizations of each of the test cases.
Square
Rectangle
Rhombus
Parallelogram
Trapezoid/Trapezium
Kite
Quadrilateral
Winning Criteria
I can't bring a computer into the exam obviously, so I need you to write the shortest code possible so I can memorize it. I need to write it into the margins and run it using TryItOfflineTM so to fit it into the margins your program needs to be as small as possible!
1 Of course I actually did :P
2 Of course I actually do :P
1
I might be able to help you with your margin problem https://xkcd.com/1381/
– Rohan Jhunjhunwala – 2017-06-22T15:24:19.837@RohanJhunjhunwala I am the new Fermat (I think that's the right person?). But nice XKCD ref :P – HyperNeutrino – 2017-06-22T15:26:18.710
is CSV allowed input? – tuskiomi – 2017-06-22T15:39:34.583
What is the partial order of specificity? – Peter Taylor – 2017-06-22T16:12:58.320
Related – Digital Trauma – 2017-06-22T16:19:13.970
can we assume first point at 0,0? – tuskiomi – 2017-06-22T16:27:20.677
@tuskiomi Please elaborate/give an example, what specific format? – HyperNeutrino – 2017-06-22T17:42:22.510
@tuskiomi No, that is not a permitted assumption. I just realized that all of the cases had that though, so I'll add another example when I get to a computer. – HyperNeutrino – 2017-06-22T17:43:17.840
@PeterTaylor Sorry, this is grade 10 math, we have not learned that yet :P What does that mean? – HyperNeutrino – 2017-06-22T17:43:48.240
The question states that square is more specific than rectangle, but doesn't state what any of the other relationships are. – Peter Taylor – 2017-06-22T17:54:25.267
@PeterTaylor ah ok. The order of specificity is as listed in the out put list, the terminology list, and the test cases list; they are all in the correct order. – HyperNeutrino – 2017-06-22T18:00:17.423
Are you sure you've got the Rectangle example right? I keep checking my math, but I get corners that are not right angles. I get angles of about 108.5 and 71.5 degrees, which makes it a parallelogram. You might use [(0, 0), (1, 1), (-1, 3), (-2, 2)] – CCB60 – 2017-06-22T22:04:21.530
@CCB60 Thanks for pointing that out. I noticed that too; the Desmos link has the coordinates you gave me but I forgot to update it >.< – HyperNeutrino – 2017-06-22T22:41:53.023
Can we assume that the points are given in counterclockwise order? – ngenisis – 2017-06-22T23:03:18.750
@ngenisis No, it could go either way. – HyperNeutrino – 2017-06-23T00:15:12.970