Prolog - 计算科赫曲线的坐标 [英] Prolog - Calculating coordinates of Koch-curve

查看:40
本文介绍了Prolog - 计算科赫曲线的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习约束编程和递归编程序言.我必须编程一个 N 级的科赫曲线,它应该从 (Sx,Sy) 开始并在 (Ex,Ey) 结束.线段,它们是正在计算中,将存储在 Ls 中.
当我尝试执行 generatelines(1,(60,0),(-60,0),Ls) 时,我得到了正确的 41 级科赫曲线的坐标:

I'm learning about constraint programming and recursive programming in Prolog. I have to programm a koch-curve of Level N which should start at (Sx,Sy) and end at (Ex,Ey).The line-segments, which are being calculated, will be stored in Ls.
When I try to execute generatelines(1,(60,0),(-60,0),Ls), I get the right 4 coordinates of the koch-curve of level 1:

[[ (60, 0), (20, 0)],
[ (20, 0), (0.0, -34.64)],
[ (0.0, -34.64), (-20, 0)], 
[ (-20, 0), (-60, 0)]]

当我尝试执行 generatelines(2,(60,0),(-60,0),Ls) 时,我得到下一个起点 (60,0) 和终点 (20,0) 之间的所有坐标.但我什至需要以下起点和终点之间的所有坐标:

When I try to execute generatelines(2,(60,0),(-60,0),Ls), then I get all the coordinates between the next start (60,0) and end point (20,0). But I even need all coordinates between following start and endpoints:

(20, 0), (0.0, -34.64),
(0.0, -34.64), (-20, 0),
(-20, 0), (-60, 0)

也是.

我的问题是,我不知道如何实现,获取坐标更高的水平.
其实我认为应该发生以下情况:

My problem is, I don't know how to implement it, to get the coordinates of a higher level.
Actually I think the following should happen:

generatelines(N1,(60,0),(20,0),Ls1)
generatelines(N1,(20,0),(0,-34.64),Ls1)
generatelines(N1,(0,-34.64),(-20,0),Ls1)
generatelines(N1,(-20,0),(-60,0),Ls1).

也许这里有人帮我解决这个问题.
谢谢

Maybe there is somebody here to help me solving this problem.
thanks

这是我到现在为止的代码:

This is the code I have until now:

- consult(library(clpfd)).
generatelines(0,_,_,Ls):- !.
generatelines(N, (Sx,Sy),(Ex,Ey),[Ls|Ls1]):-
N1 is N-1,

X2 is Sx+(Ex-Sx)/3,
Y2 is Sy+(Ey-Sy)/3,
R1 is sqrt((X2-Sx)*(X2-Sx)+(Y2-Ey)*(Y2-Ey)),
Phi1 is atan((Y2-Sy)/(X2-Sx)),
X3 is X2 +R1*cos((Phi1-240)*pi/180),
Y3 is Y2 +R1*sin((Phi1+240)*pi/180),
X4 is X2+(X2-Sx),
Y4 is Y2+(Y2-Sy),
Ls = [
      [(Sx,Sy),(X2,Y2)],
      [(X2,Y2),(X3,Y3)],
      [(X3,Y3),(X4,Y4)],
      [(X4,Y4),(Ex,Ey)]
     ],   
generatelines(N1,(Sx,Sy),(X2,Y2),Ls1).

推荐答案

您试图在一个谓词中同时做太多事情.在许多其他编程语言中,很容易忘乎所以,在一个函数或方法中投入过多;这是一个风格问题,但它可以工作.在 Prolog 中,很多东西如果没有辅助谓词就无法表达,主要是因为我们没有像其他语言那样的循环.

You are trying to do too much at once, in one predicate. In many other programming languages it is easy to get carried away and put too much into one function or method; that is a style issue, but it can work. In Prolog, many things simply cannot be expressed without auxiliary predicates, essentially because we have no loops like other languages.

这里的关键是将您的程序分解为几个不同的谓词,每个谓词都有自己的职责,如下所示:

The key here is to decompose your program into several distinct predicates each with its own responsibilities, something like this:

  • segments(S, E, Ls) 用于计算起点和终点之间的四个第一级"线段的列表LsSE
  • next_level_segments(Segments, RefinedSegments) 接受一个线段列表Segments,每个形式[P, Q],和在每对这样的端点 PQ
  • 之间生成下一级"段列表
  • iterate_level(N, InitialSegments, FinalSegments) 迭代 next_level_segments 操作 N
  • segments(S, E, Ls) for computing the list Ls of the four "first-level" line segments between start and end points S and E
  • next_level_segments(Segments, RefinedSegments) which takes a list of line segments Segments, each of the form [P, Q], and generates the "next-level" list of segments between each such pair of end points P and Q
  • iterate_level(N, InitialSegments, FinalSegments) which iterates the next_level_segments operation N times

你的最终谓词就是:

generatelines(N, S, E, Segments) :-
    segments(S, E, InitialSegments),
    iterate_level(N, InitialSegments, Segments).

当然你必须定义这些辅助谓词,但那是你的功课.

Of course you have to define these auxiliary predicates, but that is your homework.

另请注意,您实际上并未使用 clpfd 库.

Note also that you are not actually using the clpfd library.

这篇关于Prolog - 计算科赫曲线的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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