Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. object Main {
  2. implicit class Point[T <% Double](val self:(T,T)) {
  3. lazy val abs = math.sqrt(self._1 * self._1 + self._2 * self._2)
  4. def -[U <% Double](p:(U,U)) = (self._1 - p._1, self._2 - p._2)
  5. def cross[U <% Double](p:(U,U)) = self._1 * p._2 - self._2 * p._1
  6. }
  7. def crossProduct[T <% Double](polygon:List[(T,T)],points:List[(T,T)]) : List[List[Double]] =
  8. points.map { p => polygon zip(polygon.tail :+ polygon.head) map{ case(a,b) => (a - p) cross (b - p)} }
  9. def isCrossVector(xs:List[Double]) : Boolean = xs.forall(_ > 0) || xs.forall(_ < 0)
  10. def isCross[T <% Double](poly1:List[(T,T)],poly2:List[(T,T)]) : Boolean =
  11. crossProduct(poly1,poly2).exists(isCrossVector) || crossProduct(poly2,poly1).exists(isCrossVector)
  12. def isInside[T <% Double](poly1:List[(T,T)],poly2:List[(T,T)]) : Boolean =
  13. crossProduct(poly1,poly2).forall(isCrossVector) != crossProduct(poly2,poly1).forall(isCrossVector)
  14.  
  15. def input(str:String) : List[(Int,Int)] =
  16. str split(" ") map{ _.split(",") match { case Array(a,b) => (a.toInt, b.toInt)}} toList
  17.  
  18. def main(args: Array[String]): Unit = {
  19. val tri1 = input {"-1,6 2,1 4,5"}
  20. val tri2 = input {"2,-1 4,3 6,-3"}
  21.  
  22. (isCross(tri1,tri2), isInside(tri1,tri2)) match {
  23. case (true,true) => println("inside")
  24. case (true, _) => println("cross")
  25. case _ => println("outside")
  26. }
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement