Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:28:02+00:00 2026-06-17T17:28:02+00:00

Following is a fragment of a program for deducing whether or out 2 lines

  • 0

Following is a fragment of a program for deducing whether or out 2 lines intersect.
P and P2 are CPoint objects marking the start and end point of one of the 2 lines.

double m1,m2;  //slopes
double b1,b2;     //y-intercepts
double y,x;     //intersection point

m1=(max(P.y,P2.y) - min(P.y,P2.y)) /( max(P.x,P2.x) - min(P.x,P2.x) );

For some reason I’m always getting m1 to be 0. Why’s that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:28:03+00:00Added an answer on June 17, 2026 at 5:28 pm

    If your CPoint class is a point with integer coordinates, you have to do some conversion here to get the result you want. See the following demonstration of the problem. Consider two points P = (1, 4) and P2 = (5, 3):

    m1=( max(P.y,P2.y) - min(P.y,P2.y) ) / ( max(P.x,P2.x) - min(P.x,P2.x) );
         ^^^^^^^^^^^^^   ^^^^^^^^^^^^^       ^^^^^^^^^^^^^   ^^^^^^^^^^^^^
               4               3                    5               1
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       1                                   4
    

    However, in integer division, 1 / 4 is 0, but you want the result to be 0.25. The fact that the result variable has a type of double doesn’t change the value (and type) of the expression.

    To solve this problem, you have to cast the parts of your expression just before it becomes relevant that they are to be considered as non-integral numbers. In your case this are the operands of the division, so that it will be a floating point division. (Casting the result of the division will also not help.)

    m1 = static_cast<double>( max(P.y,P2.y) - min(P.y,P2.y) )
       / static_cast<double>( max(P.x,P2.x) - min(P.x,P2.x) );
    

    Note that casting the second operand is optional, as double / int always uses floating point division.

    Also note that your expression calculates the absolute value of the slope. You might want to calculate the signed slope.


    Something you can improve in your code (this won’t solve the problem above): Instead of subtracting the min of the max of the difference, just take the absolute value of the difference:

    m1 = static_cast<double>( abs(P.y - P2.y) )
       / static_cast<double>( abs(P.x - P2.x) );
    

    Since in C++, abs is a template function (in C it’s a macro, urgh…), you can also force a result type using explicit template types:

    m1 = abs<double>(P.y - P2.y)
       / abs<double>(P.x - P2.x);
    

    Also, as the calculation of a slope between two given points seems to be a commonly used function, you can implement this as a free-standing function on two CPoints:

    double absoluteSlope(const CPoint & p, const CPoint & q) {
        return abs<double>(p.y - q.y) / abs<double>(p.x - q.x);
    }
    

    Even better, to make use of C++ templates, implement it on a generic class which has the members x and y:

    template<class T>
    double absoluteSlope(const T & p, const T & q) {
        return abs<double>(p.y - q.y) / abs<double>(p.x - q.x);
    }
    

    This solution now works for your CPoint instance with integer coordinates as well as a (maybe upcoming) CPointF class with float / double coordinates.

    As already warned above, this calculates the absolute slope. To change this to a mathematically correct (signed) slope, just replace abs with static_cast:

    template<class T>
    double slope(const T & p, const T & q) {
        return static_cast<double>(p.y - q.y) / static_cast<double>(p.x - q.x);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following code fragment. /*This program demonstartes how a virtual table pointer *
I don't understand why the following program fragment gives an error: variable ‘boost::program_options::options_description desc’
I have a terribly uncomplicated test program that prints out the following numbers. i.e.
I saw the following fragment of C code in a text book and it's
I have found the following fragment in the Linux kernel (not the corresponding C
Given the following fragment of html: <fieldset> <legend>My Legend</legend> <p>Some text</p> Text to capture
i am trying to understand following fragment of javascript code <!DOCTYPE html> <html> <body>
I have to following code fragment, and no matter what I set the font-size
We have the following code fragment: char tab[2][3] = {'1', '2', '\0', '3', '4',
Consider the following code fragment in VS2010 Beta 1: let array = Array2D.zeroCreate 1000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.