Difference between revisions of "ExtractingCoordinatesFromPoint"
m |
|||
Line 1: | Line 1: | ||
− | <h2> |
+ | <h2>Extracting coordinates from a Point: PG Code Snippet</h2> |
<!-- Header for these sections -- no modification needed --> |
<!-- Header for these sections -- no modification needed --> |
||
Line 56: | Line 56: | ||
<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 |
+ | 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. |
− | + | </p> |
|
+ | <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> |
||
+ | We don't use <code>Context("Vector");</code> and <code>norm( $point[0] - $point[1] )</code> here |
||
+ | 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> |
||
<p> |
<p> |
||
− | We need to put parentheses around <code>$d1</code> and <code>$d2</code> because if <code>$d1 |
+ | We need to put parentheses around <code>$d1</code> and <code>$d2</code> 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> |
Revision as of 21:07, 13 June 2008
Extracting coordinates from a Point: PG Code Snippet
This code snippet shows the essential PG code to evaluate antderivative and general antiderivative formulas. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
This wiki page is under construction as of 6/13/08.
PG problem file | Explanation |
---|---|
loadMacros("MathObjects.pl"); |
In the initialization section, we need to include the macros file |
Context( "Point" ); push( @point, Point( random(1,5,1) , random(-5,-1,1) ) ); push( @point, Point( random(5,10,1) , random(6,11,1) ) ); ($d1, $d2) = ($point[0] - $point[1])->value; # $d1 = x1 - x2, $d2 = y1 - y2 $length = Compute("sqrt( ($d1)^2+($d2)^2 )"); $mid = ( $point[1] + $point[0] ) / 2; |
In the problem setup section of the file, we put the value of the subtraction of two Points in two variables, Alternative method: If you want to get only one of the coordinates of a Point, you can use the
We don't use Alternative method: You can use
We need to put parentheses around |
Context()->texStrings; BEGIN_TEXT Consider the two points \( $point[0] \) and \( $point[1] \). The distance between them is:\{ $length->ans_rule() \} $BR The midpoint of the line segment that joins them is:\{ $mid->ans_rule() \} $BR END_TEXT Context()->normalStrings; |
The problem text section of the file is as we'd expect. |
ANS( $length->cmp ); ANS( $mid->cmp ); |
As is the answer. |