# ExtractingCoordinatesFromPoint

## 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 `MathObjects.pl`.

```Context( "Point" );

push(@point, Point(random(1,5,1), random(-5,-1,1)));
push(@point, Point(random(5,10,1), random(6,11,1)));

# now we have two points, \$point = (x1,y1)
# and \$point = (x2,y2).
# the following makes \$d1 = x1 - x2, \$d2 = y1 - y2
(\$d1, \$d2) = (\$point - \$point)->value;

\$length = Compute("sqrt( (\$d1)^2+(\$d2)^2 )");
\$mid = ( \$point + \$point ) / 2;
```

In the problem setup section of the file, we put the value of the subtraction of two Points in two variables, `\$d1`, the x coordinate, and `\$d2`, the y coordinate. This is achieved by calling Point's `value` method, as shown.

Alternative method: If you want to get only one of the coordinates of a Point, you can use the `extract` method, for example: `\$x = \$point->extract(1);`. This gets the first coordinate of `\$point` (x) and assigns it to the variable `\$x`.

We don't use `Context("Vector");` and `norm( \$point - \$point )` here to determine length because we don't want to accept an answer like `|<5,7>-<7,8>|`.

Alternative method: You can use `\$length=norm( \$point - \$point );` with `Context("Vector");` if you want to accept answers that are valid in the Vector context (such as the absolute value of a vector).

We need to put parentheses around `\$d1` and `\$d2` in the `Compute` expression because if `\$d1 = -6`, then `-6^2 = -36`, not `36`, as desired. However, if the code is `(\$d1)^2` then that evaluates as `(-6)^2 = 36`, as desired.

```Context()->texStrings;

BEGIN_TEXT
Consider the two points \( \$point \) and \( \$point \).
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 );
```