bool PCrosshairInteraction::Draw (Painter &inPainter) {
  if (mActive) {
    float theX1 = mX;
    float theY1 = mPPlot.mMargins.mTop;
    float theX2 = mX;
    float theY2 = inPainter.GetHeight () - mPPlot.mMargins.mBottom;

    inPainter.SetLineColor (0, 0, 0);
    inPainter.DrawLine (theX1, theY1, theX2, theY2);

    PlotDataContainer &theContainer = mPPlot.mPlotDataContainer;
    long thePlotCount = theContainer.GetPlotCount ();

    for (long theI=0;theI<thePlotCount;theI++) {
      PlotDataBase *theXData = theContainer.GetXData (theI);
      PlotDataBase *theYData = theContainer.GetYData (theI);
      LegendData *theLegendData = theContainer.GetLegendData (theI);

      float theY;
      if (GetCrossPoint (theXData, theYData, theY)) {
        if (mListener) {
          float theXTarget = mPPlot.mXTrafo->TransformBack (mX);
          float theYTarget = mPPlot.mYTrafo->TransformBack (theY);
          mListener->HandleCrosshair (theI, thePlotCount, theXTarget, theYTarget);
        }
        theX1 = mPPlot.mMargins.mLeft;
        theX2 = inPainter.GetWidth ()-mPPlot.mMargins.mLeft;
        theY1 = theY2 = theY;
        PColor theC = theLegendData->mColor;
        inPainter.SetLineColor (theC.mR, theC.mG, theC.mB);
        inPainter.DrawLine (theX1, theY1, theX2, theY2);
      }
    }
  }
  return true;
}
Exemplo n.º 2
0
// 返回0:没碰撞,1:与X碰撞,2:与Y碰撞,3:与XY都碰撞
int KMovableObject::GetCollisionResult(int nSrcX, int nSrcY, int nDestX, int nDestY)
{
    BOOL XDown = false;
    BOOL XUp = false;
    BOOL YLeft = false;
    BOOL YRight = false;

    int nXDistance = -1;
    int nYDistance = -1;

    // 需要检测cell与轨迹的交面
    POINT ObjStart  = {nSrcX, nSrcY};
    POINT ObjEnd    = {nDestX, nDestY};

    POINT CellLeftDown;
    POINT CellLeftUp;
    POINT CellRightDown;
    POINT CellRightUp;

    CellLeftDown.x = nDestX - nDestX % CELL_LENGTH;
    CellLeftDown.y = nDestY - nDestY % CELL_LENGTH;

    CellLeftUp.x = nDestX - nDestX % CELL_LENGTH;
    CellLeftUp.y = nDestY - nDestY % CELL_LENGTH + CELL_LENGTH;

    CellRightDown.x = nDestX - nDestX % CELL_LENGTH + CELL_LENGTH;
    CellRightDown.y = nDestY - nDestY % CELL_LENGTH;

    CellRightUp.x = nDestX - nDestX % CELL_LENGTH + CELL_LENGTH;
    CellRightUp.y = nDestY - nDestY % CELL_LENGTH + CELL_LENGTH;

    // XDown
    if (IsLineSegmentCross(ObjStart, ObjEnd, CellLeftDown, CellRightDown))
    {
        POINT XDownIntersection;

        XDown = true;
        XDownIntersection = GetCrossPoint(ObjStart, ObjEnd, CellLeftDown, CellRightDown);
        nXDistance = g_GetDistance2((int)nSrcX, (int)nSrcY, (int)XDownIntersection.x, (int)XDownIntersection.y);
    }

    // XUp
    if (IsLineSegmentCross(ObjStart, ObjEnd, CellLeftUp, CellRightUp))
    {
        POINT XUpIntersection;

        XUp = true;
        XUpIntersection = GetCrossPoint(ObjStart, ObjEnd, CellLeftUp, CellRightUp);

        int nXUpDistance = g_GetDistance2((int)nSrcX, (int)nSrcY, (int)XUpIntersection.x, (int)XUpIntersection.y);

        if (XDown)
            nXDistance = MIN(nXUpDistance, nXDistance);
        else
            nXDistance = nXUpDistance;
    }

    // YLeft
    if (IsLineSegmentCross(ObjStart, ObjEnd, CellLeftDown, CellLeftUp))
    {
        POINT YLeftIntersection;

        YLeft = true;
        YLeftIntersection = GetCrossPoint(ObjStart, ObjEnd, CellLeftDown, CellLeftUp);
        nYDistance = g_GetDistance2((int)nSrcX, (int)nSrcY, (int)YLeftIntersection.x, (int)YLeftIntersection.y);
    }

    // YRight
    if (IsLineSegmentCross(ObjStart, ObjEnd, CellRightDown, CellRightUp))
    {
        POINT YRightIntersection;

        YRight = true;
        YRightIntersection = GetCrossPoint(ObjStart, ObjEnd, CellRightDown, CellRightUp);

        int nYRightDistance = g_GetDistance2((int)nSrcX, (int)nSrcY, (int)YRightIntersection.x, (int)YRightIntersection.y);

        if (YLeft)
            nYDistance = MIN(nYRightDistance, nYDistance);
        else
            nYDistance = nYRightDistance;
    }

    if (nXDistance >= 0 && nYDistance >= 0)
    {
        if (nXDistance < nYDistance)
            return 1;
        else if (nXDistance > nYDistance)
            return 2;
        else
            return 3;
    }
    else if (nXDistance >= 0)
        return 1;
    else if (nYDistance >= 0)
        return 2;
    else
        return 0;
}