做一个三角形转身

时间:2015-03-17 17:36:52

标签: ocaml

我试图让我的三角形转过身来,但我没有成功:/有人可以让我知道如何做到这一点吗? 这是我的代码转换船的一部分(到现在为止我可以展示船但我不能让它旋转......)

let width = 500;;
let height = 300;;
type ship = {a:point; b:point; c:point; center:point;angle:float};;
type etat = {ast:asteroid list; ship:ship } ;;


let center_ship = {x=width/2; y=height/2};;
let a_ship = {x=(center_ship.x)-5; y=(center_ship.y)-5};;
let b_ship = {x = (center_ship.x)+5; y = (center_ship.y)-5};;
let c_ship = {x = (center_ship.x); y = (center_ship.y)+20};;
let ship = { a = a_ship; b = b_ship; c = c_ship; center = center_ship; angle = 45.} ;;

let show_ship e =  
  draw_poly [|((e.ship.a.x),(e.ship.a.y));((e.ship.b.x),(e.ship.b.y));((e.ship.c.x),(e.ship.c.y))|];;


let  s = sin(45.);;
let  c = cos(45.);;
let rotate = function p -> 
  {x =int_of_float (float_of_int (ship.center.x) +. float_of_int(p.x-ship.center.x) *. c -. float_of_int(p.y-ship.center.y) *. s); 
   y =int_of_float (float_of_int (ship.center.y) +. float_of_int (p.x-ship.center.x) *. s +. float_of_int(p.y-ship.center.y) *. c)};;

let rotateLeft_ship = function ship -> {ship with a = rotate(ship.a); b= rotate(ship.b); c= rotate(ship.c)};;
let rotateLeft = function e -> {e with ship = rotateLeft_ship e.ship};;

1 个答案:

答案 0 :(得分:0)

你没有发布你的代码部分,但我猜你的动画循环看起来像

let animate state iteration_n =
  for i = 1 to iteration_n do
    show_ship (rotateLeft state)
  done

但是状态结构是不可变的,所以你的动画循环应该是一个递归函数:

let rec animate state = function
  | 0 -> ()
  | n -> show_ship state; animate (rotateLeft state) (n - 1)