Quadratic equation, a, b and c fro X and Y

Marcos Pires

New member
Joined
Oct 11, 2016
Messages
5
Hello,

I have a programming problem and although it is a simple one I'm trying to solve it with math, taking a more painful approach. I want to have an equation because the value for X and Y can vary.

The problem: Given an odd number, draw a diamond. Ex. 7
*
***
*****
*******
*****
***
*

You can note that the amount of * increase then decrease which lead me to think about quadratic equations. On this example we can think as each line as X and the amount of * as Y. Note that the last line is 7 and the line with most stars has 7 stars, meaning that these values are connected with the given number, 7.

so we have X from 1 to 7 and Y as 1,3,5,7,5,3,1

Y = 1 X = 1
Y = 3 X = 2
Y = 5 X = 3
Y = 7 X = 4
Y = 5 X = 5
Y = 3 X = 6
Y = 1 X = 7

OR

Y = 1, X = 1 or X = 7
Y = 3, X = 2 or X = 6
Y = 5, X = 3 or X = 5
Y = 7, X = 4 or X = 4

I`ve tried and tried, but I can't come up with a quadratic function that express this relation. I've tried combining all the formulas for either solve a quadratic or find the vertix (which is know since I would have X and Y at all times) but I can't come up with a, b or c. Also, I believe that 'a' will always be negative.

please help?
 
I have a programming problem and although it is a simple one I'm trying to solve it with math, taking a more painful approach. I want to have an equation because the value for X and Y can vary.

The problem: Given an odd number, draw a diamond. Ex. 7

Code:
diamond:

   *
  ***
 *****
*******
 *****
  ***
   *
You can note that the amount of * increase then decrease which lead me to think about quadratic equations. On this example we can think as each line as X and the amount of * as Y. Note that the last line is 7 and the line with most stars has 7 stars, meaning that these values are connected with the given number, 7.

so we have X from 1 to 7 and Y as 1,3,5,7,5,3,1...

I`ve tried and tried, but I can't come up with a quadratic function that express this relation.
Perhaps because the relation is not quadratic...?

Since the y-values increase by 2 (up or down) for each chance of 1 in x, this is kind-of linear, but probably better expressed as an absolute-value function. (how to graph these) ;)
 
Ahh, thank you so much, I did think about the elbow graph, but in my mind that was two opposite linear functions, which I guess is what an absolute function is.

I gonna factor this on the problem, thank you so much.
 
Again, thank you. I was able to find my function =). Going to register here for future generations

Y = -2|X - h| + k

Where, Y is the amount of * to be printed
X is the iteration number
h is the midpoint between 1 and k
k is the odd integer number provided by the user =)
 
Top