143,6 → 143,7 |
int boxplot_cnt = 0; |
int drawxml_cnt = 0; |
int numberline_cnt = 0; |
int snap_to_points_cnt = 0; |
int reply_precision = 100; /* used for precision of student answers / drawings */ |
char *rotation_center = "null";/* needs to be removed... but is used for canvas CTX based rotation*/ |
double rotationcenter[] = {0,0}; /* use for recalculating x/y values on rotation() */ |
4650,7 → 4651,7 |
/* |
@ snaptopoints x1,y1,x2,y2,x3,y3.... |
@ a userdraw object will snap to these points. |
@ the array size (e.g. the number of points) of command ''snaptopoints`` is limited by constant MAX_INT (canvasdraw.h) |
@ the array size (e.g. the number of points) of command ''snaptopoints`` is limited by constant MAX_INT (canvasdraw.h)<br/>this command may be repeated multiple times (no limit) to add points |
@ a draggable object (use command ''drag x|y|xy``) will snap to the closed of these points when dragged (mouseup) |
@ other options: use keyword ''snaptogrid``, ''xsnaptogrid`` or ''ysnaptogrid`` |
@%snaptopoints%size 400,400%xrange -5,5%yrange -5,5%snaptopoints -1,-3,-1,-2,-1,0,-1,1,-1,2,-1,3,1,-3,1,-2,1,-1,1,0,1,1,1,2,1,3%linewidth 2%points red,-1,-3,-1,-2,-1,0,-1,1,-1,2,-1,3,1,-3,1,-2,1,-1,1,0,1,1,1,2,1,3%userdraw arrows,red |
4657,7 → 4658,6 |
@%snaptopoints%size 400,400%xrange -5,5%yrange -5,5%snaptopoints -1,-3,-1,-2,-1,0,-1,1,-1,2,-1,3,1,-3,1,-2,1,-1,1,0,1,1,1,2,1,3%linewidth 3%%points blue,-1,-3,-1,-2,-1,0,-1,1,-1,2,-1,3,1,-3,1,-2,1,-1,1,0,1,1,1,2,1,3%drag xy%ftriangle -4,0,-2,3,0,0,red |
*/ |
i = 0; |
use_snap = 4; |
while( ! done ){ /* get next item until EOL*/ |
if(i > MAX_INT - 1){canvas_error("too many points in argument: repeat command multiple times to fit");} |
if(i%2 == 0 ){ |
4670,7 → 4670,15 |
i++; |
} |
decimals = find_number_of_digits(precision); |
fprintf(js_include_file,"function find_min_diff(x,y,X,Y){var diff = 100000000;var chk;var idx = 0;for(var p = 0 ; p < %d ; p++){chk = distance(x,y,X[p],Y[p]);if( chk < diff ){ diff = chk; idx = p;};};return idx;};function snap_to_points(x,y){x = px2x(x); y = px2y(y);var points = [%s];var xpoints = points[0];var ypoints = points[1];var idx = find_min_diff(x,y,xpoints,ypoints);x = xpoints[idx];y = ypoints[idx];return [x2px(x),y2px(y)];};",(int) (0.5*i),double_xy2js_array(double_data,i,decimals)); |
/* NEED AN EXTRA COUNTER VARIABLE FOR MORE THAN 2 CALLS */ |
fprintf(js_include_file,"if( temp_push_array === 'undefined' ){var temp_push_array;};if( points_to_snap_on === 'undefined' ){var points_to_snap_on;};var temp_push_array = [%s];\n",double_xy2js_array(double_data,i,decimals)); |
if( snap_to_points_cnt == 0 ){ |
fprintf(js_include_file,"points_to_snap_on = temp_push_array;function find_min_diff(x,y,X,Y){var diff = 100000000;var chk;var idx = 0;var p = 0;while(X[p] != null){chk = distance(x,y,X[p],Y[p]);if( chk < diff ){ diff = chk; idx = p;};p++;};return idx;};function snap_to_points(x,y){x = px2x(x); y = px2y(y);var xpoints = points_to_snap_on[0];var ypoints = points_to_snap_on[1];var idx = find_min_diff(x,y,xpoints,ypoints);x = xpoints[idx];y = ypoints[idx];return [x2px(x),y2px(y)];};"); |
}else{ |
fprintf(js_include_file,"points_to_snap_on[0].push.apply(points_to_snap_on[0],temp_push_array[0]);points_to_snap_on[1].push.apply(points_to_snap_on[1],temp_push_array[1]);"); |
} |
snap_to_points_cnt = 1; /* do not repeat including the js-functions...just once*/ |
use_snap = 4; |
break; |
|
case SNAPTOGRID: |