MiroJoseph

Change your Points of View

Apr 29th, 2020
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.97 KB | None | 0 0
  1. My:
  2. object ChangePoint {
  3.   def point(a: Int, b: Int):()=>List[Int]={
  4.     ()=>List(a,b)
  5.   }
  6.   def fst(pt:()=>List[Int]): Int = {
  7.     pt().head
  8.   }
  9.   def snd(pt:()=>List[Int]): Int = {
  10.     pt()(1)
  11.   }
  12.   def sqrDist(pt1:()=>List[Int], pt2:()=>List[Int]): Int = {
  13.     (math.pow(pt2()(0)-pt1()(0), 2)+math.pow(pt2()(1)-pt1()(1),2)).toInt
  14.   }
  15.   def line(pt1:()=>List[Int], pt2:()=>List[Int]): List[Int] =  List(pt1()(1)-pt2()(1),
  16.     pt2()(0)-pt1()(0),
  17.     pt1()(0)*pt2()(1) - pt2()(0)*pt1()(1) )
  18. }
  19.  
  20. Other:
  21. object ChangePoint {
  22.  
  23.   type P = () => (Int, Int)
  24.  
  25.   def point(a: Int, b: Int): P = () => (a, b)
  26.   def fst(pt: P): Int = pt()._1
  27.   def snd(pt: P): Int = pt()._2
  28.   def sqrDist(pt1: P, pt2: => P): Int =
  29.     (math.pow(fst(pt1) - fst(pt2), 2) + math.pow(snd(pt1) - snd(pt2), 2)).toInt
  30.   def line(pt1: P, pt2: P): List[Int] = {
  31.     val l = snd(pt1) - snd(pt2)
  32.     val m = fst(pt2) - fst(pt1)
  33.     val n = -(l * (fst(pt1)) + m * (snd(pt1)))
  34.     List(l, m, n)
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment