Page 1 of 1

A way to slope in PHP?

Posted: Tue Sep 04, 2007 10:12 am
by Mightywayne
I'm trying to make a place in my game for monsters to attack. The game runs on an x,y coordinate. I can't make a triangle, I don't know how.

I think it has something to do with like a slope intercept thing, not sure. Look here.

http://monbre.com/worldmap.php See New Orsa? It's #1 on the map. Look to the left where the abrupt valley begins.

I want monsters for the area that is under the abrupt valley's little triangle shape it makes.

I've tried everything, I never really learned much in math in school.

I was thinking like

Code: Select all

if (($x >= 300 || $x <= 400) && ($y >= '200' && $y <= '300'))
Which obviously doesn't work. This is how I make a square in the map, but I can't go around doing all squares.

Code: Select all

($x >= 770 && $x <= 820 && $y >= 1210 && $y <= 1290)

Posted: Tue Sep 04, 2007 10:20 am
by TheMoose
A basic math formula that might help you:
y = mx + b

m is the slope, b is the y-intercept. If m is a positive number, the line will slope upwards from left to right ( like / ), and if it is negative it will slope down from left to right ( like \ ). If m is 0, it is a horizontal line.

You can form a triangle by creating 3 lines from the above formula that have a single point in common, such as y=x, y=-x,y=5, with the points in common being (0,0), (5,5), (-5,5).

Posted: Tue Sep 04, 2007 10:24 am
by Mightywayne
Oi, that's a great idea! But I don't have any negative coords, everything's just positive. Would it still work?

Posted: Tue Sep 04, 2007 10:34 am
by TheMoose
Yes, it'll just require a little more thinking to find a formula for a negative sloped line.

The slope of a line is just "the rise over the run", meaning how much it increases in Y, over how much it increases in X, or:
Y2 - Y1
X2 - X1

With the points being (X1, Y1) and (X2, Y2) respectively. So if you have the 2 points (24,300) and (82, 192), you get
192 - 300
82 - 24

Or -108/58, or about -1.86

Using substituion, you can calculate the y-intercept (the b from previous formula) by using the same formula above, but with any of the 2 points, and (0, Y) and solving for Y:
Given (24,300) and (0,Y):

b =
300 - Y
24 - 0

PS: I feel like an algebra teacher :P

Posted: Tue Sep 04, 2007 10:41 am
by Mightywayne
PS: I feel like an algebra teacher :P
I thought you were! Jeepers, okay. So I think I got it.


So you're saying I find the highest and lowest points, at the tips... so...

700, 1290 for the bottom left, and 770, 1210 for the top.

-70, 80

Now how would I apply that to code, then?

Posted: Tue Sep 04, 2007 10:56 am
by TheMoose

Code: Select all

$x1 = 700;
$x2 = 770;
$y1 = 1290;
$y2 = 1210;
$rise = $y2 -$y1;
$run = $x2 - $x1;
$slope = $rise / $run;
// solving for B (y-intercept) yields us the formula Y1 - X1*m = Y0
$y0 = $y1 - ($x1*$slope); // y-intercept
$YPOINT = ($slope * $XPOINT) + $y0;
Granted this is for a normal Cartesian plane (a four quadrant grid with the top-right being +/+). The formula is exactly the same, but the way it appears will be different. On your grid a positively sloped line will go like \, and a negative slope will be /, because your centerpoint (0,0) is at the top left.

Posted: Tue Sep 04, 2007 11:04 am
by Mightywayne
Okay, I think I followed it correctly (mentally), but what would the if statement be? I'm sorry if I seem dumb, math was never my strong point. :( And I don't see $xpoint defined, why is this?

Posted: Tue Sep 04, 2007 11:33 am
by TheMoose
Mightywayne wrote:Okay, I think I followed it correctly (mentally), but what would the if statement be? I'm sorry if I seem dumb, math was never my strong point. :( And I don't see $xpoint defined, why is this?
It's undefined because it is the value you are trying to find based on a y value. The if statement would be more involved than just a simple X and Y being less/greater than certain values. You need to see if the point falls inside the line, and to do that, you need to find the x/y value of the point you clicked and determine where it falls in relation to the line.

Posted: Tue Sep 04, 2007 11:36 am
by Mightywayne
alright, cool. Thanks for the help!

(edit: and if anyone has any other ideas, go for it)

Posted: Tue Sep 04, 2007 11:53 am
by josa