# Progress Pie Solution in Scala

Disclaimer: I am sharing the solution only after the contest has ended.

Let’s look at the problem first – Problem Link

I am also adding a screen shot of the problem. This problem was given to me for practice by my mentor here at DC Engines on January 9th, 2017. I solved the problem that day itself but it took me a few hours to get the solution. The reason for this been my less acquaintance with the language “Scala”. My mentor gave me this problem because he wanted me to learn and implement Scala’s OOP. Yes, Scala is functional as well as OOP Language.

Now, How to solve this problem? Let’s look at the maths behind it.

I solved this problem by using simple Geometry. First let’s create an Object named “Progress Pie”.

class ProgressPieBar(){
private val x_center = 50
private val y_center = 50
private val x_sector_start = 50-x_center //shifting the origin
private val y_sector_start = 100-y_center // shifting the origin

The problem asks to check whether the point given, lies inside the progress-black region or not. So, we apply basic formula to calculate Angle of sector. We do that by following piece of code –

def angleOfSector(A:Double):Double = {
val theta = (A/100)*360
theta
}

This code calculates the region which is shaded black by using “p”. Now, make the radius line joining center and pt(50,100) as our base line and calculate angles made by given pt(x,y) wrt to this line. we use dot product to do that. Following function does it –

def angleBetweenPoint(x1:Int,y1:Int): Double = {
val lengthV2 = math.sqrt(math.pow((this.x_center-             x1).toDouble,2)+math.pow((this.y_center-y1).toDouble,2))
val modV2 = math.sqrt(math.pow(x1.toDouble,2)+math.pow(y1.toDouble,2))
val cosAngle = (this.x_sector_start.toDouble*(x1-x_center).toDouble + this.y_sector_start.toDouble*(y1-y_center).toDouble)/(this.radius.toDouble*lengthV2.toDouble)
math.acos(cosAngle).toDegrees

}

Now, we check whether the given point (x,y) lies inside the circle by following function –

def isPointInsideCircle(x:Int,y:Int):Boolean={
val dist = (this.x_center-x)*(this.x_center-x) + (this.y_center-y)*   (this.y_center-y)
true
}
else
false
}

Then, we check the color of the region in which the point lies. If the angle made by the point to the base is less that angle or the sector and if the point lies inside the circle then we say that point lies in “black” region.

def colorDeterminer(p:Int,x:Int,y:Int): String = {
if (angleBetweenPoint(x,y)<angleOfSector(p) & isPointInsideCircle(x,y)){
“black”
}
else
“white”
}

you can check the full code here – Github