object MainTest {
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
val point1 = Point(1.0, 4.0)
val point2 = Point(5.0, 1.0)
val partX = Math.abs(point1.x-point2.x) / len(point1, point2)
val partY = Math.abs(point1.y-point2.y) / len(point1, point2)
for(i in 1 until len(point1, point2).toInt()){
val x = if(point1.x < point2.x){
point1.x + partX * i.toDouble()
}else if(point1.x > point2.x){
point1.x - partX * i.toDouble()
}else{
point1.x
}
val y = if(point1.y < point2.y){
point1.y + partY * i.toDouble()
}else if(point1.y > point2.y){
point1.y - partY * i.toDouble()
}else{
point1.y
}
println("($x,$y)")
}
}
//距离
fun len(point1: Point, point2: Point): Double{
return Math.sqrt(Math.pow(Math.abs(point1.x-point2.x), 2.0) + Math.pow(Math.abs(point1.y-point2.y), 2.0))
}
}
网友评论