# Parabola

4 replies to this topic

### #1 Swast

Swast

GMC Member

• GMC Member
• 25 posts

Posted 09 June 2012 - 02:10 PM

I've tried to make a curve in my line with a parabola.

```var i;

for ( i = 0; i >= -10; i -= 1 )
{
line[  floor( x / 2 ) + i ] -= i^2;
}

for ( i = 1; i <= 10; i += 1 )
{
line[  floor( x / 2 ) + i ] += i^2;
}```

So each cell of this line-array contains the height of one "dot" of the line. With this code, the line goes wavy and the center (i=0) is the highest point but it should be the lowest.

This is how the curve should be:

but this is how gm draws it:

I've never been good at mathematics so can someone help me?
• 0

### #2 Boreal

Boreal

C++ Wackjob

• GMC Member
• 417 posts
• Version:None

Posted 09 June 2012 - 08:00 PM

1. You don't need to do two loops. A negative number squared is positive.
2. The ^ operator is not "to the power of". I believe it is bitwise XOR. You need the pow() function.
```var i;
for(i = -10; i <= 10; i += 1)
{
line[floor(x / 2) + i] -= pow(i, 2);
}
```

• 0

### #3 creators124

creators124

awesomeliciousmember

• GMC Member
• 866 posts
• Version:GM8

Posted 09 June 2012 - 08:41 PM

1. You don't need to do two loops. A negative number squared is positive.
2. The ^ operator is not "to the power of". I believe it is bitwise XOR. You need the pow() function.

```var i;
for(i = -10; i <= 10; i += 1)
{
line[floor(x / 2) + i] -= pow(i, 2);
}
```

I get this error:

___________________________________________
ERROR in
action number 1
of Draw Event
for object object0:

Error in code at line 4:
line[floor((x / 2)) + i] -= power(i, 2);
^
at position 15: Negative array index

also it is power() not pow()
• 0

### #4 Boreal

Boreal

C++ Wackjob

• GMC Member
• 417 posts
• Version:None

Posted 09 June 2012 - 11:14 PM

Yeah, sorry about that, I haven't used GM in a while. I can't fix the other error for you, other than saying that x is too small because you can't access an element of an array with a negative (or decimal) index.
• 0

### #5 Swast

Swast

GMC Member

• GMC Member
• 25 posts

Posted 10 June 2012 - 01:13 PM

Thank you! I just had to customize the code a little bit and now it works!
```var i;
for(i = -8; i <= 8; i += 1)
{
line[ floor(x / 2) + i ] += -( power( i / 2 , 2 ) );
line[ floor(x / 2) + i ] += 16;
}```

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users