ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Distance and Angle between 2 points (X,Y)
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

AndyColmes

USA
351 Posts

Posted - May 14 2016 :  04:19:59  Show Profile  Reply
How do I calculate the distance and angle between 2 given points in an image with ImageEn?

Thanks in advance.

Andy

xequte

38988 Posts

Posted - May 15 2016 :  03:59:27  Show Profile  Reply
Hi Andy

Have you tried the ruler demo:

Demos/Other/RulerBox/DRulerBox.dpr

Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - May 16 2016 :  03:27:24  Show Profile  Reply
Hi Nigel, thanks for the tip. Is there a way to do this in code?
Go to Top of Page

spetric

Croatia
308 Posts

Posted - May 17 2016 :  03:26:48  Show Profile  Reply
Hi Andy,

Here is a small function I use to get line data (line segment between two points X1,Y1 and X2, Y2):


//---------------------------------------------------------------------------
void __fastcall getLineData(double *OutLength, double *OutAngle, int x1, int y1, int x2, int y2)
{
int x_ord, y_ord;
double hypo, angle;
y_ord = y1 - y2;
x_ord = x2 - x1;
hypo = sqrt(y_ord*y_ord + x_ord*x_ord);
if (hypo == 0)
    {
    *OutAngle = 0;
    *OutLength = 0;
    return;
    }
// calculate angle as it's in the first quadrant
angle = asin((double)abs(y_ord) / hypo);
// let's check the quadrant.
// first or second quadrant?
if (y_ord >= 0)
    {
    if (x_ord < 0)
       angle = M_PI - angle;
    }
// third or fourth quadrant?
else
    {
    angle = - angle;
    if (x_ord < 0)
        angle = -M_PI - angle;
    }
*OutAngle = angle;
*OutLength = hypo;
//NOTE: output angle is in radians.
//To convert radians to degrees: angle = 180.0*angle/M_PI;
}

Go to Top of Page

spetric

Croatia
308 Posts

Posted - May 17 2016 :  03:50:01  Show Profile  Reply
Here are some functions that may come handy:


//------------------------------------------------------------------------------------------------
// Rotate array of points by some angle (in radians)
//------------------------------------------------------------------------------------------------
void __fastcall rotatePoints(TPoint *points, int points_number, TPoint *origin, double angle)
{
int ox, oy, x1, y1;
ox = origin->x;
oy = origin->y;
for (int i = 0; i < points_number; i++)
    {
    x1 = points[i].x;
    y1 = points[i].y;
    points[i].x = fast_ftol(ox + (x1 - ox)*cos(angle) - (y1 - oy)*sin(angle));
    points[i].y = fast_ftol(oy + (x1 - ox)*sin(angle) + (y1 - oy)*cos(angle));
    }
//NOTE: replace fast_ftol function with rounding function at your choice.
}
//------------------------------------------------------------------------------------------------
// Translate array of points given some radius vector (x,y) 
//------------------------------------------------------------------------------------------------
void __fastcall translatePoints(TPoint *points, int points_number, TPoint *trans)
{
for (int i = 0; i < points_number; i++)
    {
    points[i].x += trans->x;
    points[i].y += trans->y;
    }
}
//---------------------------------------------------------------------------
// Reflect array of points around arbitrarty line (given origin and angle)
//---------------------------------------------------------------------------
void __fastcall reflectPoints(TPoint *points, int points_number, TPoint *origin, double angle)
{
int ox, oy, x1, y1;
ox = origin->x;
oy = origin->y;
for (int i = 0; i < points_number; i++)
    {
    x1 = points[i].x;
    y1 = points[i].y;
    points[i].x = fast_ftol(ox + (x1 - ox)*cos(2.0*angle) + (y1 - oy)*sin(2.0*angle));
    points[i].y = fast_ftol(oy + (x1 - ox)*sin(2.0*angle) - (y1 - oy)*cos(2.0*angle));
    }
//NOTE: replace fast_ftol function with rounding function at your choice.
}
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - May 17 2016 :  10:47:34  Show Profile  Reply
Hi Spetric,

Thank you so much for sharing those functions. I will try to work with them.

Andy
Go to Top of Page

spetric

Croatia
308 Posts

Posted - May 18 2016 :  03:35:54  Show Profile  Reply
One quick note:

In function getLineData, angles range from 0 to PI for 1st and 2nd quadrant and from 0 to -PI for 3rd and 4th quadrant. You can easily adjust the angle range to suite your needs (let's say from 0 to 2PI):


// third or fourth quadrant?
else
    {
    if (x_ord <= 0)
        angle = M_PI + angle;
    else
        angle = 2.0*M_PI - angle;    
    }
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: