Skip to content

Instantly share code, notes, and snippets.

@johnchen902
Created September 19, 2015 10:42
Show Gist options
  • Save johnchen902/869140084aae4b4dc477 to your computer and use it in GitHub Desktop.
Save johnchen902/869140084aae4b4dc477 to your computer and use it in GitHub Desktop.
Judge Girl - 240. Square, Diamond, and Rectangle
#include <stdio.h>
#include <stdbool.h>
#define SWAP(T, X, Y) \
do { \
T johnchen902_swap_temp = X; \
X = Y; \
Y = johnchen902_swap_temp; \
} while(0)
int main() {
int t;
scanf("%d", &t);
while(t--) {
int x1, y1, x2, y2, x3, y3, x4, y4;
scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
if(x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4)
; // No swaps
else if(x1 + x3 == x2 + x4 && y1 + y3 == y2 + y4) {
SWAP(int, x2, x3);
SWAP(int, y2, y3);
} else if(x1 + x4 == x2 + x3 && y1 + y4 == y2 + y3) {
SWAP(int, x2, x4);
SWAP(int, y2, y4);
} else {
// not even a parallelogram
puts("other");
continue;
}
int dx1 = x1 - x2, dy1 = y1 - y2, dx2 = x3 - x4, dy2 = y3 - y4;
bool is_diamond = dx1 * dx2 + dy1 * dy2 == 0;
bool is_rectangle = dx1 * dx1 + dy1 * dy1 == dx2 * dx2 + dy2 * dy2;
if(is_diamond && is_rectangle)
puts("square");
else if(is_diamond)
puts("diamond");
else if(is_rectangle)
puts("rectangle");
else
puts("other");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment