Editing ExtractingCoordinatesFromPoint

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
<h2>Extracting coordinates from a Point: PG Code Snippet</h2>
+
<h2>Your title here: PG Code Snippet</h2>
   
 
<!-- Header for these sections -- no modification needed -->
 
<!-- Header for these sections -- no modification needed -->
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<em>This code snippet shows the essential PG code to evaluate antderivative and general antiderivative formulas. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
 
</p>
 
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
Line 27: Line 23:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("MathObjects.pl");
+
loadMacros("any macros files that are needed");
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ccffcc;padding:7px;">
 
<td style="background-color:#ccffcc;padding:7px;">
<p>
+
<p>
In the initialization section, we need to include the macros file <code>MathObjects.pl</code>.
+
To do ..(what you are doing)........., we don't have to change the
  +
tagging and documentation section of the problem file.
  +
In the initialization section, we need to include the macros file <code>-------.pl</code>.
 
</p>
 
</p>
 
</td>
 
</td>
Line 44: Line 40:
 
Context( "Point" );
 
Context( "Point" );
   
push(@point, Point(random(1,5,1), random(-5,-1,1)));
+
push( @point, Point( random(1,5,1) , random(-5,-1,1) ) );
push(@point, Point(random(5,10,1), random(6,11,1)));
+
push( @point, Point( random(5,10,1) , random(6,11,1) ) );
 
# now we have two points, $point[0] = (x1,y1)
 
# and $point[1] = (x2,y2).
 
# the following makes $d1 = x1 - x2, $d2 = y1 - y2
 
($d1, $d2) = ($point[0] - $point[1])->value;
 
   
  +
($d1, $d2) = ($point[0] - $point[1])->value; # $d1 = x1 - x2, $d2 = y1 - y2
   
  +
# Need to put () around $d1 and $d2 because if $d1 = -6, then -6^2 = -36, not 36, as desired.
 
$length = Compute("sqrt( ($d1)^2+($d2)^2 )");
 
$length = Compute("sqrt( ($d1)^2+($d2)^2 )");
 
$mid = ( $point[1] + $point[0] ) / 2;
 
$mid = ( $point[1] + $point[0] ) / 2;
Line 54: Line 52:
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
In the problem setup section of the file, we put the value of the subtraction of two Points in two variables, <code>$d1</code>, the x coordinate, and <code>$d2</code>, the y coordinate. This is achieved by calling Point's <code>value</code> method, as shown.
 
  +
We don't use <code>Context("Vector") and <code>norm( $point[0] - $point[1] )</code>
</p>
 
  +
to determine length because we don't want to accept an answer like <code>|<5,7>-<7,8>|</code> You could use <code>$length=norm( $point[0] - $point[1] );</code> with the Vector context if you didn't care about accepting answers that are valid in the Vector context.
<p>Alternative method: If you want to get only one of the coordinates of a Point, you can use the <code>extract</code> method, for example: <code>$x = $point->extract(1);</code>. This gets the first coordinate of <code>$point</code> (x) and assigns it to the variable <code>$x</code>.
 
 
</p>
 
</p>
 
<p>
 
<p>
We don't use <code>Context("Vector");</code> and <code>norm( $point[0] - $point[1] )</code> here
 
  +
Notes: on using this and related Contexts.
to determine length because we don't want to accept an answer like <code>|<5,7>-<7,8>|</code>.
 
</p>
 
<p>Alternative method: You can use <code>$length=norm( $point[0] - $point[1] );</code> with <code>Context("Vector");</code> if you want to accept answers that are valid in the Vector context (such as the absolute value of a vector).
 
</p>
 
<p>
 
We need to put parentheses around <code>$d1</code> and <code>$d2</code> in the <code>Compute</code> expression because if <code>$d1 = -6</code>, then <code>-6^2 = -36</code>, not <code>36</code>, as desired. However, if the code is <code>($d1)^2</code> then that evaluates as <code>(-6)^2 = 36</code>, as desired.
 
 
</p>
 
</p>
  +
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 78: Line 70:
   
 
BEGIN_TEXT
 
BEGIN_TEXT
Consider the two points \( $point[0] \)
+
Consider the two points \( $point[0] \) and \( $point[1] \).
and \( $point[1] \).
+
The distance between them is:\{ $length->ans_rule() \}
The distance between them is:
 
\{ $length->ans_rule() \}
 
 
$BR
 
$BR
 
The midpoint of the line segment
 
The midpoint of the line segment

Please note that all contributions to WeBWorK_wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see The WeBWorK Project wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)