在Prolog中按逆时针顺序按极角对列表进行排序 [英] Sort a list by polar angle in counterclockwise order in Prolog

查看:58
本文介绍了在Prolog中按逆时针顺序按极角对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表 L,其中包含一些诸如 range(2,3), 之类的术语,其中第一个元素是 Y 最小的术语,现在我需要排序列表 L 的其他元素(第一个元素将保持不变),我需要按逆时针顺序按极角对它们进行排序.如果两点的极角相同,那么我需要先放最接近的项.我定义了谓词angle2d/2:

I have a List L with some terms like range(2,3), where the first element is the term with the lowest Y, now I need to sort the other elements of the list L (the first element will remain unchanged), I need to sort them by polar angle in counterclockwise order. If polar angle of two points is same, then I need to put the nearest term first. I defined the predicate angle2d/2:

angle2d(A, B, R) :-
   A =.. [_,Ax,Ay],
   B =.. [_,Bx,By],
   R is atan2((Ay-By),(Ax-Bx)).

这给了我 2 点之间的逆时针角度,但我不知道如何创建谓词 sort_list/2 将我的 n-1 点(列表的第一个元素将保持不变)按他们的与列表的第一个元素(点)的逆时针角度.

that give me the counter clockwise angle between 2 points, but i didn't get how to create the predicate sort_list/2 that will sort my n-1 points (the first one element of the list will remain unchanged) by their counter clockwise angle with the first element (point) of the list.

推荐答案

我是这样做的:

sort_angle([], _).
sort_angle([_|[]], _).
sort_angle([_,_|[]], _).
sort_angle(List, SortedList) :-
   predsort(sort_me,[_|List], SortedList).

sort_me(<, A, B) :- angle2d(H,A,X), angle2d(H,B,Y), X<Y.
sort_me(>, A, B) :- angle2d(H,A,X), angle2d(H,B,Y), X>Y.
sort_me(=, A, B) :- angle2d(H,A,X), angle2d(H,B, Y), X=Y.

但我不知道,在 sort_me 谓词中如何判断 H 是我的列表的第一个元素...我该如何判断?

But I don't know, in the sort_me predicate how to tell that H is the first element of my list... how can I tell it ?

这篇关于在Prolog中按逆时针顺序按极角对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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