Skip to content
This repository
Browse code

rs_ellipse: ellipse reference points

  • Loading branch information...
commit c6bae9274a5684266853d3807e2f97984b1ac175 1 parent 9d7e7f6
Dongxu Li dxli authored
79 librecad/src/lib/engine/rs_ellipse.cpp
@@ -139,6 +139,8 @@ RS_VectorSolutions RS_Ellipse::getRefPoints() {
139 139 }
140 140 ret.push_back(data.center);
141 141 ret.appendTo(getFoci());
  142 + ret.push_back(getMajorPoint());
  143 + ret.push_back(getMinorPoint());
142 144 return ret;
143 145 }
144 146
@@ -1321,17 +1323,78 @@ double RS_Ellipse::getDirection2() const {
1321 1323 }
1322 1324
1323 1325 void RS_Ellipse::moveRef(const RS_Vector& ref, const RS_Vector& offset) {
1324   - RS_Vector startpoint = getStartpoint();
1325   - RS_Vector endpoint = getEndpoint();
  1326 + if(isArc()){
  1327 + RS_Vector startpoint = getStartpoint();
  1328 + RS_Vector endpoint = getEndpoint();
  1329 +
  1330 + // if (ref.distanceTo(startpoint)<1.0e-4) {
  1331 + if ((ref-startpoint).squared()<1.0e-8) {
  1332 + moveStartpoint(startpoint+offset);
  1333 + correctAngles();//avoid extra 2.*M_PI in angles
  1334 + return;
  1335 + }
  1336 + if ((ref-endpoint).squared()<1.0e-8) {
  1337 + moveEndpoint(endpoint+offset);
  1338 + correctAngles();//avoid extra 2.*M_PI in angles
  1339 + return;
  1340 + }
  1341 + }
  1342 + if ((ref-getCenter()).squared()<1.0e-8) {
  1343 + //move center
  1344 + setCenter(getCenter()+offset);
  1345 + return;
  1346 + }
  1347 + auto&& foci=getFoci();
  1348 + for(size_t i=0; i< 2 ; i++){
  1349 + if ((ref-foci.at(i)).squared()<1.0e-8) {
  1350 + auto&& focusNew=foci.at(i) + offset;
  1351 + //move focus
  1352 + auto&& center = getCenter() + offset*0.5;
  1353 + RS_Vector majorP;
  1354 + if(getMajorP().dotP( foci.at(i) - getCenter()) >= 0.){
  1355 + majorP = focusNew - center;
  1356 + }else{
  1357 + majorP = center - focusNew;
  1358 + }
  1359 + double d=getMajorP().magnitude();
  1360 + double c=0.5*focusNew.distanceTo(foci.at(1-i));
  1361 + double k=majorP.magnitude();
  1362 + if(k<RS_TOLERANCE2 || d < RS_TOLERANCE ||
  1363 + c >= d - RS_TOLERANCE) return;
  1364 + // DEBUG_HEADER();
  1365 + // std::cout<<__FUNCTION__<<" : moving focus";
  1366 + majorP *= d/k;
  1367 + setCenter(center);
  1368 + setMajorP(majorP);
  1369 + setRatio(sqrt(d*d-c*c)/d);
  1370 + correctAngles();//avoid extra 2.*M_PI in angles
  1371 + return;
  1372 + }
  1373 + }
1326 1374
1327   -// if (ref.distanceTo(startpoint)<1.0e-4) {
1328   - if ((ref-startpoint).squared()<1.0e-8) {
1329   - moveStartpoint(startpoint+offset);
  1375 + //move major/minor points
  1376 + if ((ref-getMajorPoint()).squared()<1.0e-8) {
  1377 + RS_Vector majorP=getMajorP()+offset;
  1378 + double r=majorP.magnitude();
  1379 + if(r<RS_TOLERANCE) return;
  1380 + double ratio = getRatio()*getMajorRadius()/r;
  1381 + setMajorP(majorP);
  1382 + setRatio(ratio);
  1383 + return;
1330 1384 }
1331   - if ((ref-endpoint).squared()<1.0e-8) {
1332   - moveEndpoint(endpoint+offset);
  1385 + if ((ref-getMinorPoint()).squared()<1.0e-8) {
  1386 + RS_Vector minorP=getMinorPoint() + offset;
  1387 + double r2=getMajorP().squared();
  1388 + if(r2<RS_TOLERANCE2) return;
  1389 + RS_Vector projected= getCenter() +
  1390 + getMajorP()*getMajorP().dotP(minorP-getCenter())/r2;
  1391 + double r=(minorP - projected).magnitude();
  1392 + if(r<RS_TOLERANCE) return;
  1393 + double ratio = getRatio()*r/getMinorRadius();
  1394 + setRatio(ratio);
  1395 + return;
1333 1396 }
1334   - correctAngles();//avoid extra 2.*M_PI in angles
  1397 +
1335 1398 }
1336 1399
1337 1400 /** whether the entity's bounding box intersects with visible portion of graphic view
9 librecad/src/lib/engine/rs_ellipse.h
@@ -239,6 +239,15 @@ class RS_Ellipse : public RS_AtomicEntity {
239 239 return data.majorP.magnitude();
240 240 }
241 241
  242 + /** @return the point by major minor radius directions */
  243 + RS_Vector getMajorPoint() const{
  244 + return data.center + data.majorP;
  245 + }
  246 + RS_Vector getMinorPoint() const{
  247 + return data.center +
  248 + RS_Vector(-data.majorP.y, data.majorP.x)*data.ratio;
  249 + }
  250 +
242 251 /** @return The minor radius of this ellipse */
243 252 double getMinorRadius() const {
244 253 return data.majorP.magnitude()*data.ratio;

0 comments on commit c6bae92

Please sign in to comment.
Something went wrong with that request. Please try again.