user3450862
user3450862

Reputation: 550

How to draw a line in OCaml?

I try to draw something in OCaml (try.ocamlpro.com). I'm not sure how to draw a line, for example x=y using the function "plot x y". Eventually I tried something like this which clearly doesn't work:

open Graphics;;
Graphics.open_graph " 200x200";;
       for i = 0 to x do
        plot i (f i)
       done
    let g x = 2*x
read_line ();;

Any help (or examples) ? Thank you.

Upvotes: 0

Views: 2761

Answers (2)

jayelm
jayelm

Reputation: 7667

There is also Graphics.lineto, which is based on a turtle-type system:

Graphics.open_graph " 200x200";;
Graphics.lineto 100 100;;

Changes the current point (defaults to 0, 0) to the supplied x y point and draws a line connecting the two.

You can set the current point with moveto : int -> int -> unit.

See the docs for more.

Upvotes: 3

Çağdaş Bozman
Çağdaş Bozman

Reputation: 2540

I can't see what is your problem. When I try your code, modified a little bit, I can draw a line as you want. First you need to initialize your window:

open Graphics;;
Graphics.open_graph " 200x200";;

Then you need to define your function f:

let f x = x + 1;;

And then just draw the line using the function plot

for i = 0 to 200 do
   plot i (f i)
done;;

Voilà !

Upvotes: 2

Related Questions