0

I'm doing MFC project now to study and given concept of this is "to reinvent the wheel". So, I should not use CRect or ArcTo and etcs. Make every basic functions by my own.

I made a getDIstance function, and I got an error that detected the shape on all the straight lines extended from each side of the shape. Considering that the error detected in Arc does not occur in Circles (and some other hints), it seems to be a matter of variables related to angles, but further estimation is difficult.

The basic form of my getDistance is like this

c++
double Line::getDistance(MyPoint point) {
    double a = m_p2.y - m_p1.y;
    double b = m_p1.x - m_p2.x;
    double c = (m_p2.x * m_p1.y) - (m_p1.x * m_p2.y);
    double dist = std:: abs((a * point.x) + (b * point.y) + c) / std::sqrt((a * a + b * b));


    return dist;
}

and it used like this

SHAPE* CMFCPresentingView::SelectOperation(MyPoint point) {
    CMFCPresentingDoc* pDoc = GetDocument();

    const int range = 5;
    SHAPE* shape = nullptr;

    if (pDoc->m_ShapeListCount == 0) return nullptr;

    for (int i = 0; i < pDoc->m_ShapeListCount; i++) {
        double dist = pDoc->m_ShapeList[i]->getDistance(point);
        if (dist < range) {
            m_Found = true;
            m_EditShapeNo = i;
            m_MoveBase = point;

            pDoc->m_ShapeList[i]->m_isSelected = true;

            shape = pDoc->m_ShapeList[i];

            return shape;
        }
    }

    return nullptr;
}

what point I've mistaken? I don't really get it.

click nearby the line and 'select' to change values on mouse move. simply, pick and move the shapes.

b4lleEr
  • 1
  • 1
  • Please see [ask] for tips writing good question titles. Writing "some error" conveys very little information about what you are asking. – JaMiT Aug 28 '23 at 03:20
  • *"I made a getDIstance function,"* -- This is a poor function description, as "get distance" does not uniquely describe what the function does. (Distance between two points is easy. Distance between arbitrary 3-dimensional objects is much harder. Distance of a block code is an entirely different kind of distance. Yet all could fall under "a getDIstance function".) – JaMiT Aug 28 '23 at 03:25
  • 1
    *"I got an error that detected the shape on all the straight lines extended from each side of the shape"* -- I'm either having trouble parsing this or not seeing why it is an error. If a line extends **from** the shape, then of course the shape is on the line. Why is it an error to detect this? Why is a distance function looking for the shape? It might be helpful to include an example consisting of input, expected output, and actual output. – JaMiT Aug 28 '23 at 03:29
  • 1
    Your life will become much easier if you implement some primitive vector operations for `MyPoint`, such as basic arithmetic, scaling, length, dot product and cross product. Fundamentally, the calculation for the perpendicular distance of a point from a line requires only primitive vector operations. – paddy Aug 28 '23 at 04:02
  • I'm sorry if I bothered guys. my getDistance method is only for 2D flat surface and intended to pick shapes follow its border. For example if it is Line instance, I expected my search function only works in line(with a littlebit of buffer) area. I couldn't think up implement the vector operations in it. I'll search and use that. – b4lleEr Aug 28 '23 at 04:13
  • 1
    It's never about bothering us. I believe the main problem with your question is you did not explain very well what the "error" was. – drescherjm Aug 28 '23 at 13:37
  • @b4lleEr *"I'm sorry if I bothered guys."* -- Instead of apologizing, your time would be better spent [edit]ing your question to improve it. ;) – JaMiT Aug 29 '23 at 02:49

0 Answers0