Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have Triangle Class, i need to compare two triangle with each other,i tried but it have given me error.What's my mistake?

What I have tried:

class Triangle
    {
        public int koxm1;
        public int koxm2;
        public int koxm3;
        public Triangle(int a, int b, int c)
        {
            this.koxm1 = a;
            this.koxm2 = b;
            this.koxm3 = c;
        }
        static public bool operator>(Erankyun a, Erankyun b)
        {
            return a.koxm1 + a.koxm2 + a.koxm3 > b.koxm1 + b.koxm2 + b.koxm3;
        }
    }


 static void Main(string[] args)
{
Erankyun e1 = new Erankyun(1,2,3);
Erankyun e2 = new Erankyun(1,4,3);

Console.WriteLine(e1 > e2); // false
}
Posted
Updated 17-Feb-18 23:49pm
v2
Comments
Maciej Los 18-Feb-18 14:23pm    
You'll need to check if triangle is possible to create: How to Determine if Three Side Lengths Are a Triangle
These lengths (1, 2, 3) cannot create a triangle.
Richard MacCutchan 19-Feb-18 4:07am    
I told you yesterday that 1+2+3 can never be greater than 1+4+3. Why do you think that reposting the question will somehow change the laws of mathematics?
johannesnestler 19-Feb-18 9:22am    
so your implementation shows the correct result: 1+2+3 > 1+4+3 == 6 > 8 -> False. So whats wrong is not your code, nor the result. Whats wrong is your understanding of comparsion of triangles. Others explained it but it seems you don't know about the very basic concepts of geometry. You may be a child? No Problem, but I think you can learn what you miss by the Internet - some wiki-reading or YouTube may help. But not asking questions on a programming forum - because you dont't have a coding error here...

Well, 1 + 2 + 3 < 1 + 4 + 3, thus e1 > e2 is false.
Moreover, A triangle is defined by 3 points; each point having two components, you cannot define a triangle with only three scalar values. You need three points, or 6 scalar values.
I would add that the entire idea to compare two triangle is flawed. Think about it: in the real world, you have several ways to compare geometric figures: you can compare their areas, or you can compare on one of their coordinates-scalar values. But there is no general concept of a geometric comparison. I.e., the set of possible coordinate values is not an ordered field per se.
You may have to take a step back and think about what you really intend from your geometric figures comparison.
 
Share this answer
 
Comments
Suren97 18-Feb-18 5:47am    
I nothing understand :)
phil.o 18-Feb-18 5:51am    
For example, don't you understand that a triangle has 3 points, thus 6 scalar values? And that you cannot define a triangle only by three scalar values?
Suren97 18-Feb-18 6:06am    
Which 6 scalar values?
phil.o 18-Feb-18 6:47am    
You are becoming an help vampire. I don't feel like you try to understand, you are just being repeating "I don't understand" but do not show any attempt to understand whatsoever.
Suren97 18-Feb-18 6:52am    
i really don't understand what you mean, i tried the same with operator+ ,it have worked, but operator> doesn't work
What defines two triangles as "bigger" and "smaller"? Is it the perimiter? The area? The location? About the only thing we know is that it's not "the sum of the three angles" becuase that is always the same value, given undistorted spacetime.

The area of a triangle is 0.5 * b * h where b is the length of the "base side", and h is the "height" from that base side of the longest perpendicular line.
The perimeter is the sum of the three line lengths which is probably what you are comparing.

But start "comparing triangles" you need to first define exactly what measure you are going to use!

And your definition of a "triangle" is not a good one: three single values can define a triangle, but not if they are integers: while 3, 4, 5 defines a valid triangle and you can work out the angles and so forth from that, that only works in special cases; not all triangles can be stored in that way: most will not have integer lengths for the three lines.

What you need to do is store three points (again, these are probably best not stored as int, but double) and decide exactly what measure you want to compare: the route have have taken so far is fundamentally flawed.
 
Share this answer
 
Comments
Suren97 18-Feb-18 6:15am    
I don't understand, what i need to do?
OriginalGriff 18-Feb-18 6:26am    
What part do you not understand? Bear in mind that I only get exactly what you type to work from, I get no other context, such as "I'm 8 years old and don't know what a triangle is" to "How do I derive my Triangle class from my Shape class, given Shape stores itself in my MySql Database?" I have no idea what you do and do not know, mush less understand!

And by the way: if those are the line lengths you are storing, what kind of triangle has line lengths (1, 3, 4)?
pt1401 18-Feb-18 16:29pm    
A very very thin one :-)
But, stretching a point, it is a triangle - the three internal angles add up to 180 degrees.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900