Click to See Complete Forum and Search --> : graphing lines using formulas


PunkSktBrdr01
11-07-2003, 05:32 PM
I have a graphing script I made (with lots of help from Jona, I believe), the Grapher (http://www.radioactiverabbit.com/graph/). The problem, though, is that to graph a line, you have to give coordinates for points on the line. I need help to make a way where the user can enter a formula for the line, and have the script automatically determine which formula was entered and graph the line. This is simple algebra I/geometry stuff, so I assume most of you are familiar with it. The formulas are:

standard form:
Ax + By = C

y-intercept form:
y = Mx + B

Then, there are the equations for vertical and horizontal lines:

vertical:
x = num

horizontal:
y = num

I tried to figure out preg_match() and preg_split(), but that stuff still confuses me. Any help is appreciated.

Jona
11-11-2003, 06:41 PM
Hey man, I'm taking Algebra 1 this year, but have no knowledge of Geometry. I'll do what I can, though.

You've got the right idea. Using RegExp's is probably the way to go... That is, assuming Ax would be replaced with an integer and By would be replaced by an integer. You might want to just use a select box and have them select the type of form they want the graph to appear in, and then it's as simple as checking to see the integers they entered in the following two textboxes, and calculate it... If you get what I mean. :p

[J]ona

PunkSktBrdr01
11-11-2003, 07:08 PM
I was going to have people choose the type of formula (from a select list, or maybe radio buttons), but if they don't enter the information correctly, it could mess up the graph. I thought it would make it a lot easier for the user if they could enter any type of formula in one text box. I'm planning on using this for some sort of extra credit, so I want to make it as user-friendly as possible. If you think it's impossible, let me know, but I do want to try to do it. I think something like this could work:

1. split the string at the = sign.
2. check to see if the x and y are on the same side.
3. if they are, it's Ax + By = num, so find two points on the line and graph it. otherwise, continue.
4. if y is on one side and x is on the other side, it is y = Mx + B, so find two points on the line and graph it. otherwise, continue.
5. if there is no x, it is y = num, so find two points on the line and graph it. otherwise, it is x = num, so find two points on the line and graph it.

I have never been able to figure out the RegExps, so I have no idea how to do all of that. Thanks, Jona. :)

Jona
11-11-2003, 07:12 PM
Okay, um, what would be the mathematical formulas, in order? (Like, what would they type in the form box and what would be an example result of that, in order?)

[J]ona

PunkSktBrdr01
11-11-2003, 09:08 PM
Input: y = 4x + 2
Output: line with slope 4 going through point (0, 2)

Input: 2x + 3y = 3
Output: line with slope -2/3 going through point (0, 1)

Input: x = 4
Output: vertical line where the x coordinate is always equal to 4

Input: y = 4
Output: horizontal line where the y coordinate is always equal to 4

Hope that is what you are looking for. I can do all of the image generation, but I just need a way to find two points for the line based on the formula. Thanks so much!

Jona
11-13-2003, 03:21 PM
Well, for the first one (to detect if that is the type of function), this would be the RegEx to detect the type of function to call.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="UTF-8">
<title>Untitled</title>
<body>
<p>
<?php
if(isset($_POST["submit"]) && isset($_POST["formula"])){
$formula = $_POST["formula"];

function dispFunc($_strY, $_strX){
echo("Y: ".$_strY."<br>\n");
echo("X: ".$_strX."<br>\n");
}

if(preg_match("/^[a-z](\s)?\=(\s)?[0-9][a-z](\s)?\+(\s)?[0-9]$/", $formula)){
$formula = explode("=",$formula);
$str["y"] = $formula[0];
$str["x"] = $formula[1];
dispFunc($str["y"],$str["x"]);
} else {
echo("Does not match...");
} }

if(!isset($_POST["submit"]) && !isset($_POST["formula"])){
?>
<form action="preg_test.php?action=submit" method="post"><div>
<input type="text" name="formula"><br>
<input type="submit" name="submit">
</div></form>
<?
}
?>
</p>
</body></html>


[J]ona