Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PointRectangle

import math.abs
object HelloWorld {
   def main(args: Array[String]) {
       val a = convertStringToPoint("0,12")
       val b = convertStringToPoint("10,12")
       val c = convertStringToPoint("10,0")
       val d = convertStringToPoint("0,0")
       val point = convertStringToPoint("2,1")
       println(ST_Contains(point,a,b,c,d))
   }
   def ST_Contains(p: Point, a: Point, b: Point, c:Point, d: Point): Boolean = {
       val areaRectangle = 0.5 * abs(
        //              ( y_A - y_C ) * ( x_D - x_B )
                        ( a.y - c.y ) * ( d.x - b.x )
        //              ( y_B - y_D ) * ( x_A - x_C )
                      + ( b.y - d.y ) * ( a.x - c.x )
                    )
       val area_ABP = area_triangle(a,b,p)
       val area_BCP = area_triangle(b,c,p)
       val area_cdp = area_triangle(c,d,p)
       val area_dap = area_triangle(d,a,p)
       
       return areaRectangle == (area_ABP+area_BCP+area_cdp+area_dap)
  }
  
  def area_triangle(a: Point, b: Point, c: Point): Double = {
      val area = 0.5 * abs( 
          (a.x * ( b.y - c.y )) + (b.x * ( c.y - a.y )) + (c.x *  (a.y - b.y))
          )
      return area
  }
  
  def convertStringToPoint(pointString: String): Point ={

    val splitString = pointString.split(",")

    Point(splitString(0).toDouble, splitString(1).toDouble)

  }
  
  case class Point(x: Double, y: Double)
  case class Rectangle(a: Point, b: Point, c: Point, d: Point)
   
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.