(************************************************************ prototype.ml Created : Thu Jun 20 00:58:19 2002 Last modified: Fri Jun 21 11:45:39 2002 Compile: ocamlopt graphics.cmxa prototype.ml -o prototype # (* $Id: prototype.ml,v 1.2 2002/06/21 02:33:47 tak Exp tak $ *) ************************************************************) (* リンクによるグラフ表現 * v1.1 とりあえず簡単な描画 * * * * * *) type point2d = Point2D of int * int type node = Node of point2d * string * (node list) let radius = 20 let threshold_of_square_distance = let tmp = 2*20+10 in tmp * tmp let background_color = Graphics.white let node_color = Graphics.magenta let label_color = Graphics.green let edge_color = Graphics.red let label_size = 20 let open_option = " 200x200+0-0" open Graphics let draw_node (Node((Point2D(x,y)),label,_)) = begin set_color node_color; fill_circle x y radius; moveto (x-5) (y-5); set_color label_color; draw_string label; end (* draw_edge from to *) let draw_edge_sub (Point2D(x1,y1)) (Node((Point2D(x2,y2)),_,_)) = let diff_x = x2-x1 and diff_y = y2-y1 in let c = (float_of_int diff_y)/.(float_of_int diff_x) and r2 = float_of_int (radius * radius) in let c2 = c *. c in let delta_x = sqrt (r2 /. (1.0 +. c2)) in (* |△x| *) let dy = int_of_float (c *. delta_x) in let dx = int_of_float delta_x in let sign_x = (if diff_x < 0 then -1 else 1) and sign_y = (if diff_y < 0 then -1 else 1) in let nx1 = x1 + sign_x * dx and ny1 = y1 + sign_y * dy and nx2 = x2 - sign_x * dx and ny2 = y2 - sign_y * dy in begin Graphics.set_color edge_color; Graphics.moveto nx1 ny1; Graphics.lineto nx2 ny2; end let draw_edges (Node(p,_,lst)) = List.iter (draw_edge_sub p) lst (************************************************************ * 描画エンジン * *) let square_distance_of_edge (Node((Point2D(x1,y1)),_,_)) (Node((Point2D(x2,y2)),_,_)) = let diff_x = x1-x2 and diff_y = y1-y2 in diff_x*diff_x + diff_y*diff_y let erase_node (Node((Point2D(x,y)),_,_)) = begin Graphics.set_color background_color; fill_circle x y radius; end (*let correct_node ((Node((Point2D(x,y)),_)) as node) =*) (* (* judge *)*) (* let threshold_of_square_distance*) (* erase_node node;*) (************************************************************ * テキスト表現からデータへ * a: b c d (b->a, c->a, d->a) *) let init () = begin open_graph open_option; set_text_size label_size; end let _ = begin init (); let n2 = Node((Point2D(100,70)),"mamewo",[]) and n3 = Node((Point2D(50,120)),"au",[]) in let n1 = Node((Point2D(20,30)),"tak",[n2;n3]) in List.iter draw_node [n1; n2; n3]; draw_edges n1; while true do () done end