bool mglnrel::clipLine(Point2d& pt1, Point2d& pt2, const Box2d& _box) { Box2d box (_box); box.normalize(); unsigned code1, code2; code1 = ClipCode(pt1, box); code2 = ClipCode(pt2, box); for ( ; ; ) { if (!(code1 | code2)) // 完全在矩形内 return true; if (code1 & code2) // 完全在矩形外 return false; float x = 0.f, y = 0.f; unsigned code; if (code1) // 起点不在矩形内 code = code1; else // 终点不在矩形内 code = code2; if (code & 0x1000) // 上 { x = pt1.x + (pt2.x - pt1.x) * (box.ymax - pt1.y) / (pt2.y - pt1.y); y = box.ymax; } else if (code & 0x0100) // 下 { x = pt1.x + (pt2.x - pt1.x) * (box.ymin - pt1.y) / (pt2.y - pt1.y); y = box.ymin; } else if (code & 0x0001) // 左 { y = pt1.y + (pt2.y - pt1.y) * (box.xmin - pt1.x) / (pt2.x - pt1.x); x = box.xmin; } else if (code & 0x0010) // 右 { y = pt1.y + (pt2.y - pt1.y) * (box.xmax - pt1.x) / (pt2.x - pt1.x); x = box.xmax; } if (code == code1) { pt1.x = x; pt1.y = y; code1 = ClipCode(pt1, box); } else { pt2.x = x; pt2.y = y; code2 = ClipCode(pt2, box); } } }
void MgCmdManagerImpl::getBoundingBox(Box2d& box, const MgMotion* sender) { MgCommand* cmd = sender->cmds()->getCommand(); Box2d selbox; if (cmd && strcmp(cmd->getName(), MgCmdSelect::Name()) == 0) { MgCmdSelect* sel = (MgCmdSelect*)cmd; selbox = sel->getBoundingBox(sender); } box = selbox.isEmpty() ? Box2d(sender->pointM, 0, 0) : selbox; box *= sender->view->xform()->modelToDisplay(); box.normalize(); }