Hello Everyone! Thanks for taking the time to view my first forum post! I was on a code challenge site called codeingame.com today and came across a problem that is absolutely eating my brain alive! So I thought I would ask for some help before I hurt myself. I've listed the instructions it gave me below.
Instructions A console program must be created that will take 4 pairs of random numbers. These pairs represent coordinate points on a graph. The program written must return whether the given pairs make a square. For example, the following ints are passed into the console program ordered in x, y coordinate format (0,0) (0,-2) (-2,-2) (-2,0) The result would be true because they form a square.
I realize that this is more a math problem given that the only thing to do is parse the input string but for the life of me I can't get it. Maybe it's been too long since that last math class.
Thanks for the help and I hope someone can get a good brain teaser out of this.
--Update--
Ok so after finding the department head for math at the university I work in i.t at I've managed to learn more about how to solve the problem from a math standpoint, unfortunately it get a little more complicated than what I originally anticipated because the first step requires us finding out what plane the square might be on (if the same one). I'll throw some stuff up here once I've had a chance to settle in from work. Cheers!
--Update--
Alright, I've made a GUI with some field inputs now it's time for the math. More soon.
This post was last modified: 01-09-2016, 06:08 PM by Zultralord.
First of all welcome to the forum and sorry for not helping you out earlier!
Oh my... My math skills are very rusty (rarely need it in IT, and if I need to I use Google or Calc.exe) but let me see what I can recall. Here's what I think you should do:
Before you read anything else: we're in 2D space, right? Makes this easier than 3D and you won't have to worry about the plane they're in, since there is only one. (Unless I misunderstood what you meant, English isn't my primary language so I'm not very familiar with math terms.)
I'll call the points p1, p2, p3 and p4.
Before doing anything else, check if:
- more than two points share their x or y
- at least two points share both x and y
If either is true, we're definitely not looking at a square (in 2D space). We can also confirm the second case by checking if the distance between two points is 0 instead of checking x and y.
One of the following must be true, otherwise it's not a square:
Code:
(distanceTo2 == distanceTo3 and distanceTo4 == sqrt(pow(distanceTo2,2) + pow(distanceTo3,2)))
or
(distanceTo2 == distanceTo4 and distanceTo3 == sqrt(pow(distanceTo2,2) + pow(distanceTo4,2)))
or
(distanceTo3 == distanceTo4 and distanceTo2 == sqrt(pow(distanceTo3,2) + pow(distanceTo4,2)))
This tells us that the distance from p1 to the other points qualify (but not confirm) it as square, due to two vectors being the same length and, if it is a square, the diagonal distance to the furthest point would be correct as well.
But as of now, assuming this square (in our example "code" above that is the second case) we have only confirmed that:
- p2 and p4 lie somewhere on the red circle
- p3 lies somewhere on the purple circle
However by confirming the distance from p1 to p2 and p4 we can triangulate the only possible position of p3 for it to be a square. As seen here, we know that both p1 and p3 must have the same distance to both p2 and p4:
So we have to calculate the distance from p3 to p2 and p4. This way we can assure that (on a 2D plane) the quadrangle is at least a diamond, since we confirmed in the beginning that p1 and p3 are not the same point.
...Well, at least I am certain I am looking at one. Here goes hoping I didn't mess up, that'd be embarrassing :O
----
Edit: I guess an alternative approach would be to check the angles of the vectors in relation to each other.
Careful: I'm not 100% sure if this if correct. Well, that applies to both approaches, but the thought just crossed my mind and I figured I'd write it down for you ;)
You'd start out looking at p1-p2, make that our reference vector. (red)
Check if the other two vectors p1-p3 (blue) and p1-p4 (green) are at a 45° or 90° angle (origin p1). Two should be at a 90° angle to each other and both at a 45° angle to the remaining one.
See here: Green & red -> 90°
Green & blue as well as red & blue -> 45°
If the opposite end (p3 in this example) also has a 90° angle between the vectors (p3-p2 and p3-p4) it guarantees it to be a rectangle, and due to the 45° angle of the diagonal line it's a confirmed square.
Hope that was understandable and correct :P
This post was last modified: 01-08-2016, 08:40 AM by Mazzn.
Visit me at mazzn.net & blog.mazzn.net!
//This is very important :) Self.KeepImproving(true);