The_Oddler

Oplossing Prolog Tapispleine Pablo

Oct 29th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.30 KB | None | 0 0
  1. house(5,4).
  2.  
  3. wall(v,2,3,4).
  4. wall(v,3,0,1).
  5. wall(v,4,0,1). %wall(v,x,y,y2)
  6. wall(h,2,3,5). %wall(h,y,x,x2)
  7.  
  8. color(1,1,green).
  9. color(1,2,red).
  10. color(3,1,red).
  11. color(4,0,blue).
  12.  
  13. % Unifies Wall with a wall in the given part.
  14. % Fail when no walls are found and doesn't include wall
  15. % that lie on the edge of the given part.
  16. % Unspecified behaviour when Wall is already unified when calling this clause
  17. wall_in_part(part(XCo1,YCo1,XCo2,YCo2),Wall) :-
  18.     wall(Orient,Ca,Cb,S),
  19.     (Orient = v ->
  20.         %Ca = x; Cb = y; S = y2
  21.         % x moet strikt binnen Part liggen
  22.         Ca > XCo1, %wall mag niet tegen de rand liggen
  23.         Ca < XCo2,
  24.         % onderste y moet strikt onder de bovenkant beginnen
  25.         % eind-y moet strik boven de onderkant liggen
  26.         S > YCo1,
  27.         Cb < YCo2
  28.     ; %assumes only v and h will be used as orientation for walls.
  29.         %Ca = y; Cb = x; S = x2
  30.         Cb < XCo2, %mag niet beginnen aan rechter kant
  31.         S > XCo1,
  32.         % y moet strikt binnen Part liggen
  33.         Ca > YCo1, %mag niet op rand liggen
  34.         Ca < YCo2
  35.     ),
  36.     Wall = wall(Orient,Ca,Cb,S).
  37.  
  38. color_in_part(part(XCo1,YCo1,XCo2,YCo2),Color) :-
  39.     color(X,Y,Color),
  40.     X >= XCo1, %colors CAN touch the left-bottom
  41.     Y >= YCo1,
  42.     X < XCo2, %but not the right-top since then they would go outside the part
  43.     Y < YCo2.
  44.     %Color = Col. %directly get Color when fetching one.
  45.  
  46. single_color_present(Part) :-
  47.     findall(Color,
  48.         color_in_part(Part,Color),
  49.         DColors),
  50.     sort(DColors,Colors), % to remove doubles, there's also a build-in that only removes doubles and doesn't sort, but can't remember it now, I should use that one when I remember it
  51.     length(Colors,ColorCount),
  52.     ColorCount =< 1.
  53.  
  54. part_has_wall(Part) :-
  55.     findall(Wall,
  56.         wall_in_part(Part,Wall),
  57.         Walls),
  58.     length(Walls,WallCount),
  59.     WallCount >= 1.
  60.  
  61. cut_plan(CuttingPlan,Parts) :-
  62.     house(Top,Right),
  63.     cut_part(part(0,0,Top,Right), CuttingPlan, UParts),
  64.     sort(UParts,Parts).
  65.  
  66. cut_part(Part, [], Parts) :- %no cutting so cuttingplan is empty.
  67.     \+ wall_in_part(Part,_),
  68.     % no more walls in this part so it can't be cut further
  69.     % check if there is only one color in this part
  70.     single_color_present(Part),
  71.     %if so it's a good part so we should add it to parts.
  72.     Parts = [Part].
  73. cut_part(Part, CuttingPlan, Parts) :-
  74.     wall_in_part(Part,wall(WOr,WOa,_,_)), %kies wall om op te knippen
  75.     part(XCo1,YCo1,XCo2,YCo2) = Part, %get info of this part
  76.     % generate cut based on wall and current part
  77.     ( WOr = v ->
  78.         Cut = cut(v,WOa,YCo1,YCo2),
  79.         % make two new parts
  80.         Part1 = part(XCo1,YCo1,WOa,YCo2), % links eerst, dan rechts
  81.         Part2 = part(WOa,YCo1,XCo2,YCo2)
  82.     ;   %again assumes only orientations are 'v' and 'h'
  83.         %similar to a vertical cut
  84.         Cut = cut(h,WOa,XCo1,XCo2),
  85.         % make two new parts
  86.         % onderste eerst, dan bovenste
  87.         Part1 = part(XCo1,YCo1,XCo2,WOa),
  88.         Part2 = part(XCo1,WOa,XCo2,YCo2)
  89.     ),
  90.     % cut the new parts
  91.     cut_part(Part1,CuttingPlan1,Parts1),
  92.     cut_part(Part2,CuttingPlan2,Parts2),
  93.     % add everything together.
  94.     append(CuttingPlan1,CuttingPlan2,CuttingPlanTemp),
  95.     CuttingPlan = [Cut|CuttingPlanTemp],
  96.     append(Parts1,Parts2,Parts).
Advertisement
Add Comment
Please, Sign In to add comment