--- loncom/homework/functionplotresponse.pm	2011/11/20 00:34:52	1.78
+++ loncom/homework/functionplotresponse.pm	2011/11/21 02:34:58	1.80
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # Functionplot responses
 #
-# $Id: functionplotresponse.pm,v 1.78 2011/11/20 00:34:52 www Exp $
+# $Id: functionplotresponse.pm,v 1.80 2011/11/21 02:34:58 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -768,9 +768,9 @@ sub start_functionplotvectorrule {
    }
    $label=~s/\W//gs;
    unless ($label) {
-      $label='V'.$Apache::functionplotresponse::counter;
+      $label='R'.$Apache::functionplotresponse::counter;
    } else {
-      $label='V'.$label;
+      $label='R'.$label;
    }
    if ($Apache::functionplotresponse::splineorder{$label}) {
        &Apache::lonxml::error(&mt('Rule indices must be unique.'));
@@ -822,8 +822,8 @@ sub start_functionplotvectorrule {
            $nottippoint,
            &Apache::lonxml::get_param('length',$parstack,$safeeval),
            &Apache::lonxml::get_param('angle',$parstack,$safeeval),
-           &Apache::lonxml::get_param('lengthpercenterror',$parstack,$safeeval),
-           &Apache::lonxml::get_param('anglepercenterror',$parstack,$safeeval),
+           &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
+           &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
           )));
    } elsif ($target eq 'edit') {
         $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Rule').
@@ -847,9 +847,9 @@ sub start_functionplotvectorrule {
                                      $token,'16').
              &Apache::edit::text_arg('Angle:','angle',
                                      $token,'16').
-             &Apache::edit::text_arg('Percent error length:','lengthpercenterror',
+             &Apache::edit::text_arg('Absolute error length:','lengtherror',
                                      $token,'8').
-             &Apache::edit::text_arg('Percent error angle:','anglepercenterror',
+             &Apache::edit::text_arg('Absolute error angle:','angleerror',
                                      $token,'8').
              &Apache::edit::end_row();
   } elsif ($target eq 'modified') {
@@ -864,7 +864,7 @@ sub start_functionplotvectorrule {
                                                  $safeeval,'index','vector','attachpoint','notattachpoint',
                                                            'tailpoint','tippoint','nottailpoint','nottipoint',
                                                            'length','angle',
-                                                           'lengthpercenterror','anglepercenterror');
+                                                           'lengtherror','angleerror');
     if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
    }
    return $result;
@@ -892,9 +892,9 @@ sub start_functionplotvectorsumrule {
    }
    $label=~s/\W//gs;
    unless ($label) {
-      $label='V'.$Apache::functionplotresponse::counter;
+      $label='R'.$Apache::functionplotresponse::counter;
    } else {
-      $label='V'.$label;
+      $label='R'.$label;
    }
    if ($Apache::functionplotresponse::splineorder{$label}) {
        &Apache::lonxml::error(&mt('Rule indices must be unique.'));
@@ -914,9 +914,8 @@ sub start_functionplotvectorsumrule {
            $object,
            &Apache::lonxml::get_param('length',$parstack,$safeeval),
            &Apache::lonxml::get_param('angle',$parstack,$safeeval),
-           &Apache::lonxml::get_param('lengthpercenterror',$parstack,$safeeval),
-           &Apache::lonxml::get_param('lengthabserror',$parstack,$safeeval),
-           &Apache::lonxml::get_param('anglepercenterror',$parstack,$safeeval),
+           &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
+           &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
           )));
    } elsif ($target eq 'edit') {
         $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Sum Rule').
@@ -928,11 +927,9 @@ sub start_functionplotvectorsumrule {
                                      $token,'16').
              &Apache::edit::text_arg('Sum vector angle:','angle',
                                      $token,'16').
-             &Apache::edit::text_arg('Percent error length:','lengthpercenterror',
-                                     $token,'8').
-             &Apache::edit::text_arg('Absolute error length:','lengthabserror',
+             &Apache::edit::text_arg('Error length:','lengtherror',
                                      $token,'8').
-             &Apache::edit::text_arg('Percent error angle:','anglepercenterror',
+             &Apache::edit::text_arg('Error angle:','angleerror',
                                      $token,'8').
              &Apache::edit::end_row();
   } elsif ($target eq 'modified') {
@@ -940,7 +937,7 @@ sub start_functionplotvectorsumrule {
     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                                                  $safeeval,'index','object',
                                                            'length','angle',
-                                                           'lengthpercenterror','lengthabserror','anglepercenterror');
+                                                           ,'lengtherror','angleerror');
     if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
    }
    return $result;
@@ -1432,15 +1429,47 @@ sub vectorcheck {
    my ($label,$type,$id,$vector,
        $attachpoint,$notattachpoint,
        $tailpoint,$tippoint,$nottailpoint,$nottippoint,
-       $length,$angle,$lengthpercenterror,$anglepercenterror)=split(/\:/,$rule);
+       $length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
    &addlog("Vector Rule $label for vector ".$vector);
-
+   if ($length ne '') {
+      &addlog("Checking for length $length with error $lengtherror");
+      $length=&Apache::run::run($length,$safeeval);
+      &addlog("Length evaluated to $length");
+      my $thislength=&plotvectorlength($id,$vector);
+      &addlog("Found length $thislength");
+      if (abs($thislength-$length)>$lengtherror) {
+         &setfailed($label);
+         return 0;
+      }
+   }
+   if ($angle ne '') {
+      &addlog("Checking for angle $angle with error $angleerror");
+      $angle=&Apache::run::run($angle,$safeeval);
+      &addlog("Angle evaluated to $angle");
+      my $thisangle=&plotvectorangle($id,$vector);
+      &addlog("Found angle $thisangle");
+      my $anglediff=abs($thisangle-$angle);
+      &addlog("Angle difference: $anglediff");
+      if ($anglediff>360.-$anglediff) {
+         $anglediff=360.-$anglediff;
+      }
+      &addlog("Smallest angle difference: $anglediff");
+      if ($anglediff>$angleerror) {
+         &setfailed($label);
+         return 0;
+      }
+   }
+   &addlog("Rule $label passed.");
+   return 1;
 }
 
 sub sumcheck {
    my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
-   my ($label,$type,$id,$object,$length,$angle,$lengthpercenterror,$lengthabserror,$anglepercenterror)=split(/\:/,$rule);
+   my ($label,$type,$id,$object,$length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
    &addlog("Vector Sum Rule $label for vectors attached to ".$object);
+
+   &addlog("Rule $label passed.");
+   return 1;
 }
 
 #
@@ -1542,10 +1571,7 @@ sub functionplotrulecheck {
      unless (&compare_rel($relationship,$value,$integral,$tol)) {
         &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
         &addlog("Rule $label failed.");
-        my $hintlabel=$label;
-        $hintlabel=~s/^R//;
-        push(@Apache::functionplotresponse::failedrules,$hintlabel);
-        &addlog("Set hint condition $hintlabel");
+        &setfailed($label);
         return 0;
      } 
    } else {
@@ -1611,8 +1637,7 @@ sub checklength {
 }
 
 sub setfailed {
-   my ($label)=@_;
-   my $hintlabel=$label;
+   my ($hintlabel)=@_;
    $hintlabel=~s/^R//;
    push(@Apache::functionplotresponse::failedrules,$hintlabel);
    &addlog("Set hint condition $hintlabel");