查找列表中两个点之间的最大距离(方案) [英] Finding maximum distance between two points in a list (scheme)

查看:88
本文介绍了查找列表中两个点之间的最大距离(方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试从一个点列表中编写一个函数,该函数返回从点p到我的点列表中最远离p的点的距离.我的要点列表如下:

I'm currently trying to write a function from a list of points that returns the distance from a point p to a point in my point list that is farthest away from p. My list of points are the following:

((2 . 4) (3 . 6) (5 . 12) (-4 . 3) (8.4 . 9) (0 . -1))

我也做了一些抽象,以检索一般的car和cdr(以方便查看代码),以及列表本身的car和cdr.

I have also made some abstractions to retrieve the general car and cdr (for easier visibility in the code), as well as the car and cdr of the list itself.

(define (get-x p) (car p)
(define (get-y p) (car p)


(define (get-first-point pt-list)
        (get-x pt-list))

(define (get-rest-points pt-list)
        (get-y pt-list))                                                                                         

我还已经写出了一个通用距离公式,可用于选择的任何两个点(请记住,我将car和cdr分别抽象为get-x和get-y).

I have also already written out a generalized distance formula that I can use for any two points that I choose (keep in mind that I abstracted car and cdr to be get-x and get-y respectively).

(define (distance a b)
    (if (or (null? a) (null? b))
        0
        (sqrt (+ (expt (- (get-x a)
                          (get-x b))  2) 
                 (expt (- (get-y a) 
                          (get-y b))  2)))))

现在有了这个,我知道我需要比较整个点集的各种距离并选择最大返回值.我有一个部分解决方案,但不是一个对每个问题都正确的解决方案.

Now that I have this, I know that I need to compare the various distances throughout my entire set of points and choose the maximum return value. I have a partial solution, but not one that's correct for every point.

(define (max-distance p pt-list)
    (if (null? pt-list)
        0
        (max (distance p (get-first-point pt-list)) ; 1
             (distance p (get-first-point (get-rest-points pt-list))) ; 2
             (distance p (get-first-point (get-rest-points (get-rest-points pt-list)))) ; 3
             (distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points pt-list))))) ; 4
             (distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points (get-rest-points pt-list)))))) ; 5
             (distance p (get-first-point (get-rest-points (get-rest-points (get-rest-points (get-rest-points (get-rest-points pt-list))))))) ; 6
    )
)

)

您可能可以了解我(非常)试图做的事情的主旨,但这就是为什么我需要帮助.

You can probably get the gist of what I'm (horribly) attempting at doing, but this is why I need the help.

推荐答案

计算到 car 的距离.距离是最大的,还是距 points cdr 的最大距离.结果是遍历点列表的简单递归算法.

Compute the distance to the car of points. Either that distance is the maximum or the maximum from the cdr of points is. Result is a simple recursive algorithm traversing the list of points.

(define (max-distance-p p points)
  (if (null? points)
      0
      (max (distance p (car points))
           (max-distance-p p (cdr points)))))

如果将 points 作为空列表传递,则返回 0 ;否则,返回 0 .这可能有点可疑.如果是这样:

The 0 is returned if you pass in points as an empty list; that might be a bit questionable. If so:

(define (max-distance-p p points)
  (assert (not (null? points)))
  (if (null? (cdr points))
      (distance p (car points))
      (max (distance p (car points))
           (max-distance-p p (cdr points)))))

这篇关于查找列表中两个点之间的最大距离(方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆