70 lines
1.6 KiB
Swift
70 lines
1.6 KiB
Swift
func crossProduct(_ X: [Int], _ Y: [Int], _ Z: [Int]) -> Int {
|
|
let XY = [Y[0] - X[0], Y[1] - X[1]]
|
|
let XZ = [Z[0] - X[0], Z[1] - X[1]]
|
|
|
|
let val = XY[0]*XZ[1] - XY[1]*XZ[0]
|
|
|
|
return val > 0 ? 1 : val < 0 ? -1 : 0
|
|
}
|
|
|
|
func pointSort(_ x1: Int, _ y1: Int, _ x2: Int, _ y2: Int)-> ([Int], [Int]) {
|
|
if x1 < x2 {
|
|
return ([x1, y1], [x2,y2])
|
|
}
|
|
else if x1 > x2 {
|
|
return ([x2, y2], [x1,y1])
|
|
}
|
|
else if y1 < y2 {
|
|
return ([x1, y1], [x2,y2])
|
|
}
|
|
else {
|
|
return ([x2, y2], [x1,y1])
|
|
}
|
|
}
|
|
|
|
func pointCompare(_ X: [Int], _ Y: [Int]) -> Bool {
|
|
if X[0] < Y[0] {
|
|
return true
|
|
}
|
|
else if X[0] == Y[0] && X[1] <= Y[1] {
|
|
return true
|
|
}
|
|
else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func main() -> Int {
|
|
guard let line1 = readLine(),
|
|
let line2 = readLine()
|
|
else { return -1 }
|
|
|
|
let L1 = line1.split(separator: " ").compactMap{Int($0)}
|
|
let L2 = line2.split(separator: " ").compactMap{Int($0)}
|
|
|
|
let x1 = L1[0], y1 = L1[1], x2 = L1[2], y2 = L1[3]
|
|
let x3 = L2[0], y3 = L2[1], x4 = L2[2], y4 = L2[3]
|
|
|
|
let (A, B) = pointSort(x1, y1, x2, y2)
|
|
let (C, D) = pointSort(x3, y3, x4, y4)
|
|
|
|
let ABCD = crossProduct(A, B, C) * crossProduct(A, B, D)
|
|
let CDAB = crossProduct(C, D, A) * crossProduct(C, D, B)
|
|
|
|
if ABCD == 0 && CDAB == 0 {
|
|
if pointCompare(A, D) && pointCompare(C, B) {
|
|
return 1
|
|
}
|
|
else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
if ABCD <= 0 && CDAB <= 0 {
|
|
return 1
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
print(main()) |