File:
[LON-CAPA] /
loncom /
homework /
imageresponse.pm
Revision
1.63:
download - view:
text,
annotated -
select for diffs
Wed Mar 2 22:26:36 2005 UTC (19 years, 5 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
lonnet::repcopy() and lonnet::repcopy_userfile() now return strings rather than constants defined in Apache::Constants. This allows lonnet::repcopy() to be used for cloning in batch course creation run from a command line script. If Apache::Constants are to be returned by PerlHandlers these should be set in the appropriate handler rather than in subroutines in lonnet called by the handler, based on the string response from lonnet::repcopy().
1:
2: # The LearningOnline Network with CAPA
3: # image click response style
4: #
5: # $Id: imageresponse.pm,v 1.63 2005/03/02 22:26:36 raeburn Exp $
6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
29: #FIXME LATER assumes multiple possible submissions but only one is possible
30: #currently
31:
32: package Apache::imageresponse;
33: use strict;
34: use Image::Magick();
35: use Apache::randomlylabel();
36: use Apache::londefdef();
37: use Apache::Constants qw(:common :http);
38: use Apache::lonlocal;
39:
40: BEGIN {
41: &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
42: }
43:
44: sub start_imageresponse {
45: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
46: my $result;
47: #when in a radiobutton response use these
48: &Apache::lonxml::register('Apache::imageresponse',
49: ('foilgroup','foil','text','image','rectangle',
50: 'polygon','conceptgroup'));
51: push (@Apache::lonxml::namespace,'imageresponse');
52: my $id = &Apache::response::start_response($parstack,$safeeval);
53: undef(%Apache::response::foilnames);
54: if ($target eq 'meta') {
55: $result=&Apache::response::meta_package_write('imageresponse');
56: } elsif ($target eq 'analyze') {
57: my $part_id="$Apache::inputtags::part.$id";
58: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
59: }
60: return $result;
61: }
62:
63: sub end_imageresponse {
64: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
65: &Apache::response::end_response;
66: pop @Apache::lonxml::namespace;
67: &Apache::lonxml::deregister('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
68: my $result;
69: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
70: undef(%Apache::response::foilnames);
71: return $result;
72: }
73:
74: %Apache::response::foilgroup=();
75: sub start_foilgroup {
76: %Apache::response::foilgroup=();
77: $Apache::imageresponse::conceptgroup=0;
78: &Apache::response::pushrandomnumber();
79: return '';
80: }
81:
82: sub getfoilcounts {
83: my ($parstack,$safeeval)=@_;
84:
85: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
86: # +1 since instructors will count from 1
87: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
88: #if (&Apache::response::showallfoils()) { $max=$count; }
89: return ($count,$max);
90: }
91:
92: sub whichfoils {
93: my ($max)=@_;
94: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
95: my @names = @{ $Apache::response::foilgroup{'names'} };
96: my @whichopt =();
97: while ((($#whichopt+1) < $max) && ($#names > -1)) {
98: &Apache::lonxml::debug("Have $#whichopt max is $max");
99: my $aopt;
100: # if (&Apache::response::showallfoils()) {
101: # $aopt=0;
102: # } else {
103: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
104: # }
105: &Apache::lonxml::debug("From $#names elms, picking $aopt");
106: $aopt=splice(@names,$aopt,1);
107: &Apache::lonxml::debug("Picked $aopt");
108: push (@whichopt,$aopt);
109: }
110: return @whichopt;
111: }
112:
113: sub prep_image {
114: my ($image,$mode,$name)=@_;
115: my $part=$Apache::inputtags::part;
116: my $respid=$Apache::inputtags::response['-1'];
117: my $id=&Apache::loncommon::get_cgi_id();
118: my (%x,$i);
119: $x{"cgi.$id.BGIMG"}=&Apache::lonnet::escape($image);
120: my ($x,$y)=split(/:/,$Apache::lonhomework::history{"resource.$part.$respid.submission"});
121: #draws 2 xs on the image at the clicked location
122: #one in white and then one in red on top of the one in white
123: if (defined($x) && $x=~/\S/ && defined($y) && $y =~/\S/ && !&Apache::response::show_answer()) {
124: my $length = 6;
125: my $width = 1;
126: my $extrawidth = 2;
127: my $xmin=($x-$length);
128: my $xmax=($x+$length);
129: my $ymin=($y-$length);
130: my $ymax=($y+$length);
131:
132: $x{"cgi.$id.OBJTYPE"}.='LINE:';
133: $i=$x{"cgi.$id.OBJCOUNT"}++;
134: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
135: "FFFFFF",($width+$extrawidth)));
136: $x{"cgi.$id.OBJTYPE"}.='LINE:';
137: $i=$x{"cgi.$id.OBJCOUNT"}++;
138: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
139: "FFFFFF",($width+$extrawidth)));
140: $x{"cgi.$id.OBJTYPE"}.='LINE:';
141: $i=$x{"cgi.$id.OBJCOUNT"}++;
142: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymin),($xmax),($ymax),
143: "FF0000",($width)));
144: $x{"cgi.$id.OBJTYPE"}.='LINE:';
145: $i=$x{"cgi.$id.OBJCOUNT"}++;
146: $x{"cgi.$id.OBJ$i"}=join(':',(($xmin),($ymax),($xmax),($ymin),
147: "FF0000",($width)));
148: }
149: if ($mode eq 'answer') {
150: my $width = 1;
151: my $extrawidth = 2;
152: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
153: foreach my $area (@areas) {
154: if ($area=~/^rectangle:/) {
155: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
156: $i=$x{"cgi.$id.OBJCOUNT"}++;
157: my ($x1,$y1,$x2,$y2)=
158: ($area=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
159: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"FFFFFF",
160: ($width+$extrawidth)));
161: $x{"cgi.$id.OBJTYPE"}.='RECTANGLE:';
162: $i=$x{"cgi.$id.OBJCOUNT"}++;
163: $x{"cgi.$id.OBJ$i"}=join(':',($x1,$y1,$x2,$y2,"00FF00",$width));
164: } elsif ($area=~/^polygon:(.*)/) {
165: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
166: $i=$x{"cgi.$id.OBJCOUNT"}++;
167: $x{"cgi.$id.OBJ$i"}=join(':',("FFFFFF",($width+$extrawidth)));
168: $x{"cgi.$id.OBJEXTRA$i"}=$1;
169: $x{"cgi.$id.OBJTYPE"}.='POLYGON:';
170: $i=$x{"cgi.$id.OBJCOUNT"}++;
171: $x{"cgi.$id.OBJ$i"}=join(':',("00FF00",$width));
172: $x{"cgi.$id.OBJEXTRA$i"}=$1;
173: }
174: }
175: }
176: &Apache::lonnet::appenv(%x);
177: return $id;
178: }
179:
180: sub displayfoils {
181: my ($target,@whichopt) = @_;
182: my $result ='';
183: my $name;
184: my $temp=1;
185: foreach $name (@whichopt) {
186: $result.=$Apache::response::foilgroup{"$name.text"};
187: &Apache::lonxml::debug("Text is $result");
188: if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
189: my $image=$Apache::response::foilgroup{"$name.image"};
190: &Apache::lonxml::debug("image is $image");
191: if ( $target eq 'web' && $image !~ /^http:/ ) {
192: $image=&clean_up_image($image);
193: }
194: &Apache::lonxml::debug("image is $image");
195: if ( &Apache::response::show_answer() ) {
196: if ($target eq 'tex') {
197: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
198: } else {
199: my $token=&prep_image($image,'answer',$name);
200: $result.="<img src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
201: }
202: } else {
203: if ($target eq 'tex') {
204: $result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
205: } else {
206: my $id=$Apache::inputtags::response['-1'];
207: my $token=&prep_image($image);
208: my $temp=1;
209: $result.="<input type=\"image\" name=\"HWVAL_$id:$temp\" ".
210: "src=\"/adm/randomlabel.png?token=$token\" /><br />\n";
211: }
212: }
213: $temp++;
214: }
215: return $result;
216: }
217:
218: sub clean_up_image {
219: my ($image)=@_;
220: if ($image =~ /\s*<img\s*/) {
221: ($image) = ($image =~ /src\s*=\s*[\"\']([^\"\']+)[\"\']/i);
222: if ($image !~ /^http:/) {
223: $image=&Apache::lonnet::hreflocation('',$image);
224: }
225: if (!$image) {
226: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
227: }
228: } else {
229: $image=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$image);
230: if (&Apache::lonnet::repcopy($image) ne 'OK') {
231: $image='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
232: }
233: }
234: return $image;
235: }
236:
237: sub gradefoils {
238: my (@whichopt) = @_;
239: my $x;
240: my $y;
241: my $result;
242: my $id=$Apache::inputtags::response['-1'];
243: my $temp=1;
244: foreach my $name (@whichopt) {
245: $x=$ENV{"form.HWVAL_$id:$temp.x"};
246: $y=$ENV{"form.HWVAL_$id:$temp.y"};
247: &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
248: if (defined($x) && defined($y) &&
249: defined(@{ $Apache::response::foilgroup{"$name.area"} })) {
250: my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
251: my $grade="INCORRECT";
252: foreach my $area (@areas) {
253: &Apache::lonxml::debug("Area is $area for $name");
254: $area =~ m/([a-z]*):/;
255: &Apache::lonxml::debug("Area of type $1");
256: if ($1 eq 'rectangle') {
257: $grade=&grade_rectangle($area,$x,$y);
258: } elsif ($1 eq 'polygon') {
259: $grade=&grade_polygon($area,$x,$y);
260: } else {
261: &Apache::lonxml::error("Unknown area style $area");
262: }
263: &Apache::lonxml::debug("Area said $grade");
264: if ($grade eq 'APPROX_ANS') { last; }
265: }
266: &Apache::lonxml::debug("Foil was $grade");
267: if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
268: if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
269: &Apache::lonxml::debug("Question is $result");
270: $temp++;
271: }
272: }
273: my $responsestr="$x:$y";
274: my $part=$Apache::inputtags::part;
275: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
276: if ($result
277: && $Apache::lonhomework::type eq 'survey') { $result='SUBMITTED'; }
278: &Apache::response::handle_previous(\%previous,$result);
279: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$responsestr;
280: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$result;
281: return '';
282: }
283:
284: sub end_foilgroup {
285: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
286: my $result='';
287: my @whichopt;
288: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
289: $target eq 'analyze') {
290: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
291: if ($count>$max) { $count=$max }
292: &Apache::lonxml::debug("Count is $count from $max");
293: @whichopt = &whichfoils($max);
294: if ($target eq 'web' || $target eq 'tex') {
295: $result=&displayfoils($target,@whichopt);
296: } elsif ($target eq 'grade') {
297: if ( defined $ENV{'form.submitted'}) { &gradefoils(@whichopt); }
298: } elsif ( $target eq 'analyze') {
299: &Apache::response::analyze_store_foilgroup(\@whichopt,
300: ['text','image','area']);
301: }
302: } elsif ($target eq 'edit') {
303: $result=&Apache::edit::end_table();
304: }
305: &Apache::response::poprandomnumber();
306: return $result;
307: }
308:
309: sub start_conceptgroup {
310: $Apache::imageresponse::conceptgroup=1;
311: %Apache::response::conceptgroup=();
312: return '';
313: }
314:
315: sub end_conceptgroup {
316: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
317: $Apache::imageresponse::conceptgroup=0;
318: my $result;
319: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
320: $target eq 'analyze') {
321: &Apache::response::pick_foil_for_concept($target,
322: ['area','text','image'],
323: \%Apache::hint::image,
324: $parstack,$safeeval);
325: } elsif ($target eq 'edit') {
326: $result=&Apache::edit::end_table();
327: }
328: return $result;
329: }
330:
331: sub insert_foil {
332: return '
333: <foil>
334: <image></image>
335: <text></text>
336: <rectangle></rectangle>
337: </foil>
338: ';
339: }
340:
341: $Apache::imageresponse::curname='';
342: sub start_foil {
343: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
344: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
345: $target eq 'analyze') {
346: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
347: if (!$name) {
348: &Apache::lonxml::error("Foils without names exist. This can cause problems to malfunction.");
349: $name=$Apache::lonxml::curdepth;
350: }
351: if (defined($Apache::response::foilnames{$name})) {
352: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
353: }
354: $Apache::response::foilnames{$name}++;
355: if ( $Apache::imageresponse::conceptgroup
356: #&& !&Apache::response::showallfoils()
357: ) {
358: push(@{ $Apache::response::conceptgroup{'names'} }, $name);
359: } else {
360: push(@{ $Apache::response::foilgroup{'names'} }, $name);
361: }
362: $Apache::imageresponse::curname=$name;
363: }
364: return '';
365: }
366:
367: sub end_foil {
368: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
369: my $result;
370: if ($target eq 'edit') {
371: $result=&Apache::edit::end_table();
372: }
373: return $result;
374: }
375:
376: sub start_text {
377: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
378: my $result='';
379: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
380: &Apache::lonxml::startredirection;
381: } elsif ($target eq 'edit') {
382: my $descr=&Apache::lonxml::get_all_text('/text',$parser);
383: $result=&Apache::edit::tag_start($target,$token,'Task Description').
384: &Apache::edit::editfield($token->[1],$descr,'Text',60,2).
385: &Apache::edit::end_row();
386: } elsif ($target eq "modified") {
387: $result=$token->[4].&Apache::edit::modifiedfield('/text',$parser);
388: }
389: return $result;
390: }
391:
392: sub end_text {
393: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
394: my $result;
395: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
396: my $name = $Apache::imageresponse::curname;
397: if ( $Apache::imageresponse::conceptgroup
398: #&& !&Apache::response::showallfoils()
399: ) {
400: $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
401: } else {
402: $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
403: }
404: } elsif ($target eq 'edit') {
405: $result=&Apache::edit::end_table();
406: }
407: return $result;
408: }
409:
410: sub start_image {
411: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
412: my $result='';
413: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
414: &Apache::lonxml::startredirection;
415: } elsif ($target eq 'edit') {
416: my $bgimg=&Apache::lonxml::get_all_text('/image',$parser);
417: $Apache::edit::bgimgsrc=$bgimg;
418: $Apache::edit::bgimgsrcdepth=$Apache::lonxml::curdepth;
419:
420: $result=&Apache::edit::tag_start($target,$token,'Clickable Image').
421: &Apache::edit::editline($token->[1],$bgimg,'Image Source File',40);
422: $result.=&Apache::edit::browse(undef,'textnode').' ';
423: $result.=&Apache::edit::search(undef,'textnode').
424: &Apache::edit::end_row();
425: } elsif ($target eq "modified") {
426: $result=$token->[4].&Apache::edit::modifiedfield('/image',$parser);
427: }
428: return $result;
429: }
430:
431: sub end_image {
432: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
433: my $result;
434: my $name = $Apache::imageresponse::curname;
435: if ($target eq 'web') {
436: my $image = &Apache::lonxml::endredirection();
437: &Apache::lonxml::debug("original image is $image");
438: if ( $Apache::imageresponse::conceptgroup
439: #&& !&Apache::response::showallfoils()
440: ) {
441: $Apache::response::conceptgroup{"$name.image"} = $image;
442: } else {
443: $Apache::response::foilgroup{"$name.image"} = $image;
444: }
445: } elsif ($target eq 'analyze') {
446: my $image = &Apache::lonxml::endredirection();
447: if ( $Apache::imageresponse::conceptgroup
448: #&& !&Apache::response::showallfoils()
449: ) {
450: $Apache::response::conceptgroup{"$name.image"} = $image;
451: } else {
452: $Apache::response::foilgroup{"$name.image"} = $image;
453: }
454: } elsif ($target eq 'edit') {
455: $result=&Apache::edit::end_table();
456: } elsif ($target eq 'tex') {
457: my $src = &Apache::lonxml::endredirection();
458: my ($path,$file) = &Apache::londefdef::get_eps_image($src);
459: my ($height_param,$width_param)=
460: &Apache::londefdef::image_size($src,0.3,$parstack,$safeeval);
461: $Apache::response::foilgroup{"$name.image"} ='\vskip 0 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
462: }
463: return $result;
464: }
465:
466: sub start_rectangle {
467: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
468: my $result='';
469: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
470: $target eq 'analyze') {
471: &Apache::lonxml::startredirection;
472: } elsif ($target eq 'edit') {
473: my $coords=&Apache::lonxml::get_all_text('/rectangle',$parser);
474: $result=&Apache::edit::tag_start($target,$token,'Rectangle').
475: &Apache::edit::editline($token->[1],$coords,'Coordinate Pairs',40).
476: &Apache::edit::entercoord(undef,'textnode',undef,undef,'box').
477: &Apache::edit::end_row();
478: } elsif ($target eq "modified") {
479: &Apache::edit::deletecoorddata();
480: $result=$token->[4].&Apache::edit::modifiedfield('/rectangle',$parser);
481: }
482: return $result;
483: }
484:
485: sub grade_rectangle {
486: my ($spec,$x,$y) = @_;
487: &Apache::lonxml::debug("Spec is $spec");
488: my ($x1,$y1,$x2,$y2)=($spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/);
489: &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
490: if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
491: if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
492: if (($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)) {
493: return 'APPROX_ANS';
494: }
495: return 'INCORRECT';
496: }
497:
498: sub end_rectangle {
499: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
500: my $result;
501: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
502: $target eq 'analyze') {
503: my $name = $Apache::imageresponse::curname;
504: my $area = &Apache::lonxml::endredirection;
505: $area=~s/\s//g;
506: &Apache::lonxml::debug("out is $area for $name");
507: if ( $Apache::imageresponse::conceptgroup
508: #&& !&Apache::response::showallfoils()
509: ) {
510: push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
511: } else {
512: push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
513: }
514: } elsif ($target eq 'edit') {
515: $result=&Apache::edit::end_table();
516: }
517: return $result;
518: }
519:
520: sub start_polygon {
521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
522: my $result='';
523: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
524: $target eq 'analyze') {
525: &Apache::lonxml::startredirection;
526: } elsif ($target eq 'edit') {
527: my $coords=&Apache::lonxml::get_all_text('/polygon',$parser);
528: $result=&Apache::edit::tag_start($target,$token,'Polygon').
529: &Apache::edit::editline($token->[1],$coords,'Coordinate list',40).
530: &Apache::edit::entercoord(undef,'textnode',undef,undef,'polygon').
531: &Apache::edit::end_row();
532: } elsif ($target eq "modified") {
533: $result=$token->[4].&Apache::edit::modifiedfield('/polygon',$parser);
534: }
535: return $result;
536: }
537:
538: sub grade_polygon {
539: my ($spec,$x,$y) = @_;
540: &Apache::lonxml::debug("Spec is $spec");
541: $spec=~s/^polygon://;
542: my @polygon;
543: foreach my $coord (split('-',$spec)) {
544: my ($x,$y)=($coord=~m/\(([0-9]+),([0-9]+)\)/);
545: &Apache::lonxml::debug("x $x y $y");
546: push @polygon, {'x'=>$x,'y'=>$y};
547: }
548: #make end point start point
549: push @polygon, $polygon[0];
550: # cribbed from
551: # http://geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
552: my $crossing = 0; # the crossing number counter
553:
554: # loop through all edges of the polygon
555: for (my $i=0; $i<$#polygon; $i++) { # edge from V[i] to V[i+1]
556: if ((($polygon[$i]->{'y'} <= $y)
557: && ($polygon[$i+1]->{'y'} > $y)) # an upward crossing
558: ||
559: (($polygon[$i]->{'y'} > $y)
560: && ($polygon[$i+1]->{'y'} <= $y))) { # a downward crossing
561: # compute the actual edge-ray intersect x-coordinate
562: my $vt = ($y - $polygon[$i]->{'y'})
563: / ($polygon[$i+1]->{'y'} - $polygon[$i]->{'y'});
564: if ($x < $polygon[$i]->{'x'} + $vt *
565: ($polygon[$i+1]->{'x'} - $polygon[$i]->{'x'})) { # x<intersect
566: $crossing++; # a valid crossing of y=P.y right of P.x
567: }
568: }
569: }
570:
571: # 0 if even (out), and 1 if odd (in)
572: if ($crossing%2) {
573: return 'APPROX_ANS';
574: } else {
575: return 'INCORRECT';
576: }
577: }
578:
579: sub end_polygon {
580: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
581: my $result;
582: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex' ||
583: $target eq 'analyze') {
584: my $name = $Apache::imageresponse::curname;
585: my $area = &Apache::lonxml::endredirection;
586: $area=~s/\s*//g;
587: &Apache::lonxml::debug("out is $area for $name");
588: if ( $Apache::imageresponse::conceptgroup
589: #&& !&Apache::response::showallfoils()
590: ) {
591: push @{ $Apache::response::conceptgroup{"$name.area"} },"polygon:$area";
592: } else {
593: push @{ $Apache::response::foilgroup{"$name.area"} },"polygon:$area";
594: }
595: } elsif ($target eq 'edit') {
596: $result=&Apache::edit::end_table();
597: }
598: return $result;
599: }
600: 1;
601: __END__
602:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>