Groovy web console

subscribe to the feed Subscribe
to this
site
exercise_01 (via #groovywebconsole)
tweet this snippet Tweet
this
script

exercise_01

Published 3 weeks ago by efi with tags TwoPoints
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.transform.ToString

@ToString(includeNames=true)
class Point {    
     Point(int x, int y)  {
        this.x = x
        this.y = y  
     } 
     int x
     int y
}

@ToString(includeNames=true)
class TwoPoints  {
     TwoPoints(Point a, Point b)  {
        this.a = a
        this.b = b  
     }    
    Point a
    Point b
}

float calcDistance(TwoPoints TwoPoints ) {
   if(TwoPoints.a.x == TwoPoints.b.x) {  result = 0
  } else
  if(TwoPoints.a.y == TwoPoints.b.y) {
  result = abs(TwoPoints.a.x - TwoPoints.b.x)
  }
  else {
  result = sqrt(((TwoPoints.a.x - TwoPoints.b.x)**2 + (TwoPoints.a.y - TwoPoints.b.y)**2))
  }
    
   return result 
}

Point p1 = new Point(5,7)
Point p2 = new Point(5,74)
//println p1
//println p2

TwoPoints twoPoints  = new TwoPoints(p1,p2)  
//println twoPoints  


float distance = calcDistance(twoPoints)
println distance