Created
July 27, 2017 08:52
-
-
Save zonoise/2a6bf566214319a093e4358248f808a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
def main(args: Array[String]): Unit = { | |
printAreas(2.0,2.0,2.0) | |
printAreas(2,2,2) | |
//val t = hoge(46,39,6) | |
val t = hoge(3,1,3) | |
println(t) | |
printAreas(t._1,t._2,t._3) | |
hoge(1,1,1) | |
printAreas( ) | |
} | |
def hoge(a:Double,b:Double,c:Double): Tuple3[Double,Double,Double] ={ | |
val t = new Tuple3((a + b )/ 2.0, | |
(b + c) / 2.0, | |
(a + 2 * b + c) / 2.0 - b) | |
return t | |
} | |
def printAreas(radiusA:Double,radiusB:Double,distance:Double):Unit = { | |
val circleA = new Circle(radiusA) | |
val circleB = new Circle(radiusB) | |
val overlapArea = getOverlapArea(circleA.radius,circleB.radius,distance); | |
println( | |
circleA.area()-overlapArea, | |
circleB.area()-overlapArea, | |
overlapArea | |
) | |
} | |
def getOverlapArea(a:Double ,b:Double,distance:Double): Double = { | |
if((a + b) < distance){ | |
return 0 | |
} | |
val cosA = cosineFormula(distance,b,a) | |
//println(math.toDegrees(math.acos(cosA))) | |
val areaSectorA = math.pow(a,2) * math.acos(cosA) / 2 | |
val cosB = cosineFormula(distance,a,b) | |
//println(math.toDegrees(math.acos(cosB))) | |
val areaSectorB = math.pow(a,2) * math.acos(cosB) / 2 | |
val cos = cosineFormula(a,b,distance) | |
val sinA = cosToSin(cos) | |
val areaTriangle = triangleAreaFromTwoEdgeAndSin(a,b,sinA) | |
//println(areaSectorA,areaSectorB,areaTriangle) | |
val halfArea = areaSectorA + areaSectorB - areaTriangle | |
return halfArea * 2 | |
} | |
/** 余弦定理 */ | |
private def cosineFormula(a:Double,b:Double,c:Double): Double ={ | |
/** | |
* cos alpha = ( a**2 + b**2 - c**2 ) / 2 * a * b | |
*/ | |
val result = (math.pow(a,2) + math.pow(b,2) - math.pow(c,2)) / (2*a*b) | |
return result | |
} | |
private def cosToSin(cos:Double):Double = { | |
val result = math.sqrt( 1 - math.pow(cos,2) ) | |
return result | |
} | |
private def triangleAreaFromTwoEdgeAndSin(e1:Double, e2:Double ,sinValue:Double):Double={ | |
return 1.0/2 * e1 * e2 * sinValue | |
} | |
} | |
class Circle(val radius:Double) { | |
def area() :Double = { | |
math.pow(radius , 2) * math.Pi | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment