triangle help??

This is a discussion on triangle help?? within the C++ Programming forums, part of the General Programming Boards category; Write a graphics program that prompts the user to enter graphics objects using the mouse or/and the keyboard. Draw these ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    triangle help??

    Write a graphics program that prompts
    the user to enter graphics objects using the mouse or/and the keyboard.
    Draw these graphics objectsq make calculations and print out the results .
    Your program must be tested with an example given after every task.
    13. Enter a triangle and find a point placed inside it

    what i do:

    Code:
    #include <cmath>
    using namespace std;
    #include "ccc_win.h"
    
    int ccc_win_main()
    
    {
        
        //imput 3 points
        Point a=cwin.get_mouse("Click on the first point:");
        cwin << a;
        Point b=cwin.get_mouse("Click on the second point:");
        cwin << b;
        Point c=cwin.get_mouse("Click on the third point:");
        cwin << c;
        
        // lines
        Line l1(a,b);
        Line l2(b,c);
        Line l3(c,a);
        
        // geting coordinates of the points
        double x1=a.get_x();
        double y1=a.get_y();
        double x2=b.get_x();
        double y2=b.get_y();
        double x3=c.get_x();
        double y3=c.get_y();
        
        // determine the length of the external sides
        double dist1=sqrt(sqrt(x1-x2) + sqrt(y1-y2));
        double dist2=sqrt(sqrt(x2-x3) + sqrt(y2-y3));
        double dist3=sqrt(sqrt(x3-x1) + sqrt(y3-y1));
        
        //result triangle
        cwin << a << b << c << l1 << l2 << l3;
        
        Point d=cwin.get_mouse("Click on the trinagle:");
        
        double x4=d.get_x();
        double y4=d.get_y();
        
        Line l4(a,d);
        Line l5(b,d);
        Line l6(c,d);
        
        // determine the length of the internal lines
        double dist4=sqrt(sqrt(x1-x4) + sqrt(y1-y4));
        double dist5=sqrt(sqrt(x2-x4) + sqrt(y2-y4));
        double dist6=sqrt(sqrt(x3-x4) + sqrt(y3-y4));
        
        // limits the point in the range of the triangle
    
    double s=sqrt((dist1+dist2-dist3)*(dist1-dist2+dist3)*(-dist1+dist2+dist3)*(dist1+dist2+dist3))*0.25;
    double s1=sqrt((dist1+dist5-dist4)*(dist1-dist5+dist4)*(-dist1+dist5+dist4)*(dist1+dist5+dist4))*0.25;
    double s2=sqrt((dist2+dist6-dist5)*(dist2-dist6+dist5)*(-dist2+dist6+dist5)*(dist2+dist6+dist5))*0.25;
    double s3=sqrt((dist3+dist4-dist6)*(dist3-dist4+dist6)*(-dist3+dist4+dist6)*(dist3+dist4+dist6))*0.25;  
     
    
    if (fabs(s-(s1+s2+s3))<1e-6)
    cwin << d;
    else
    cwin <<  Message (Point(0,0),"the point must be inside the triangle!");
        
     
     return 0;   
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    So what's your question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    the question is

    Quote Originally Posted by Salem View Post
    So what's your question?
    My question is:

    why when i click inside the triangle(the 4th click) the point doesn't appear?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since when has this:
    Code:
    double dist4=sqrt(sqrt(x1-x4) + sqrt(y1-y4));
    been the distance formula? You're probably getting NaN all over the place.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Since when has this:
    Code:
    double dist4=sqrt(sqrt(x1-x4) + sqrt(y1-y4));
    been the distance formula? You're probably getting NaN all over the place.
    what is NaN? I didn't understand you very well.I got this formula from another place in the net,

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    NaN = Not a number.

    You didn't get that formula. You got the formula sqrt((x1-x4)*(x1-x4) + (y1-y4)*(y1-y4)), which is rather different. (That is to say, squares inside, not square roots.)

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    NaN = Not a number.

    You didn't get that formula. You got the formula sqrt((x1-x4)*(x1-x4) + (y1-y4)*(y1-y4)), which is rather different. (That is to say, squares inside, not square roots.)
    THANKS A LOT MAN, YOUR ADVICE WAS SUPER USEFUL TO ME!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 02:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 01:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21