# Class to print Cartesian Plane and # to graph equations as f(x) class Cartesian constructor: (@center_x , @center_y) -> @axis_color=black @axis_weight=.5 @axis_label_color=black @axis_label=true # Step size per point plotted @graph_resolution=.01 # Line Graphing Options @graph_line=false @graph_line_weight=1 @graph_line_color=green # Point Graphing Options @graph_points=true @graph_point_color=green @graph_point_weight=1 @grid_weight=1 @grid_color=lightgray @pixels_per_unit=50 draw_axis: -> stroke @axis_color, @axis_weight # Draw Axis Lines Y moveTo 0 , @center_y line 500, 0 # Draw Axis Lines Y moveTo @center_x , 0 line 0, 500 draw_grid: -> # Draw X lines stroke @grid_color, @grid_weight for pos in [ @center_y-@pixels_per_unit .. 0 ] by -@pixels_per_unit moveTo 0 , pos line 500, 0 for pos in [ @center_y+@pixels_per_unit .. 500 ] by @pixels_per_unit moveTo 0 , pos line 500, 0 for pos in [ @center_x-@pixels_per_unit .. 0 ] by -@pixels_per_unit moveTo pos , 0 line 0, 500 for pos in [ @center_x+@pixels_per_unit .. 500 ] by @pixels_per_unit moveTo pos , 0 line 0, 500 draw_labels : -> color @axis_label_color bold true font 15 moveTo @center_x , @center_y move (500-@center_x)-7 , -3 text "X" moveTo @center_x , 0 move 7, 15 text "Y" draw_plane: -> this.draw_grid() this.draw_axis() this.draw_labels() graph: ( equation ) -> if (@graph_resolution <= 0) @graph_resolution = 1 x_min=-@center_x/@pixels_per_unit x_max=(500-@center_x)/@pixels_per_unit if @graph_line # Don't draw first line stroke 0 for x_in in [ x_min .. x_max ] by @graph_resolution y_out = equation(x_in) lineTo (x_in*@pixels_per_unit)+@center_x , -(y_out*@pixels_per_unit)+@center_y moveTo (x_in*@pixels_per_unit)+@center_x , -(y_out*@pixels_per_unit)+@center_y stroke @graph_line_color, @graph_line_weight if @graph_points # Dont add stroke thickness to point stroke 0 for x_in in [ x_min .. x_max ] by @graph_resolution y_out = equation(x_in) moveTo (x_in*@pixels_per_unit)+@center_x , -(y_out*@pixels_per_unit)+@center_y color @graph_point_color circle @graph_point_weight my_graph = new Cartesian( 250, 250 ) # Draw the plane my_graph.draw_plane() # Graph some functions my_graph.graph (x) -> Math.sin(x) my_graph.graph (x) -> Math.cos(x) my_graph.graph (x) -> Math.tan(x)