Hello dear people from C++.com
Once again i need your help guys.
I have a question.
How can i return an equation as a equation (i mean the things from school like 5x+3y=15 or something like this)
I've got equation of line in geometry
here is the function
double Line::Equation() {
char x,y;
return (x-x1)*(y2-y1)-(y-y1)*(x2-x1);
}
where x1,y1,x2,y2 are all private members in class Line.
Anyway i would like to return an whole equation not a value.
I need those integers with x after them and thos with y to calculate the distance between the line and the beginnig of the Oxy (coordinate plane) Im not sure about some mathemtical terms but i hope you understand me.
Please help :) and thank to all of you :)
I don't exactly understand what you're trying to do either, but I can see that you don't really understand how C++ expressions work.
C++ is an imperative language, that means that when you write an expression, that expression is code get evaluated right there on the spot. An expression in C++ is not data structure, it is code, for example you can't store the expression "x+y", it's code, not data.
What you need to do in order to "store" the equation, is develop a data structure capable of representing the equation, and return that. The most obvious way would be to, as Galik pointed out, use a string to store the equation, but that's not the best way to do it. That would require you to parse the string to do what you want later, and it would be better to make an "Equation" class which holds the equation in a more easily operated on form.
Galik
stravant
In a pure mathematical way if we have the coordinates of two points (x1,y1) (x2,y2)
for example lets say the coordinate of the first points
x1=2
y1=2
and the coordinates of the second point
x2=1
y2=1
that equation (x-x1)*(y2-y1)-(y-y1)*(x2-x1) /it is equation because it is equal to zero/ will be
(x-2)*(1-2)-(y-2)*(1-2)= (x-2)*(-1)-(y-2)*(-1) = -x+2+y-2
and i want to print that -x+2+y-2.
helios
We didn't learn any functional language this year in my university. If this can't be done that way there must be other way to calculate the distance and that would be my problem :)
thanks for all the replies :)
Okay, in that case it's a reasonably simple problem. So, your problem is really reducing the equation into it's simplest form so that you can display it.
What you need to do then is solve for a, b, and c in ax + by + c, in terms of x1, x2, y1, and y2. I don't know what the simplest way to approach that is, it's above my level of math. It _looks_ like you should be able to do it with a system of equations, but having 4 pieces of data and solving for only 3 variables makes things complicated, right?
Once you have a, b, and c it's just a matter of formatting them into a std::string, which you can do either with std::stringstream or printf, whatever you prefer.
stravant,s answer is ok.
you can let equation (x-x1)*(y2-y1)-(y-y1)*(x2-x1) change into any format you prefer then use printf function to fromat it ,an example as "printf(szFormat, "-%dx + %d + %dy - %d", y1-y2, (y1-y2)*x1, x1-x2, (x1-x2)*y1)" ,the result of szFormat will be "-1x + 2 + 1y -2";about 1 i aslo can,t find out a good way to throw off.