美文网首页
两点一线求坐标

两点一线求坐标

作者: Wocus | 来源:发表于2018-08-16 09:02 被阅读70次
    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))
        }
    }
    
    

    相关文章

      网友评论

          本文标题:两点一线求坐标

          本文链接:https://www.haomeiwen.com/subject/xzcnbftx.html