void TextEdit::cursorPositionChanged() { //alignmentChanged(textEdit->alignment()); QColor col = Qt::red; QTextCharFormat fmt; fmt.setForeground(col); QTextCursor cursor = textEdit->textCursor(); cout<<"#TextEdit::cursorPositionChanged:"; cout<<"cursor.selectionStart:"<<cursor.selectionStart()<<","<<cursor.selectionEnd(); int selectionStart=cursor.selectionStart(), selectionEnd=cursor.selectionEnd(); cursor.mergeCharFormat(fmt); colorChanged(col); if(!cursor.hasSelection()) return; QTextBlock block=cursor.block(); int blockStart=block.position(); QTextLayout* layout=cursor.block().layout(); QTextLine layoutLine=layout->lineForTextPosition(selectionStart-blockStart); cout<<"layout line:"; int lineNumber= layoutLine.lineNumber(); QPoint blockOffset=layout->position().toPoint(); QPoint lineOffset=layoutLine.position().toPoint(); printPoint(blockOffset); printPoint(lineOffset); QPoint linePoint(blockOffset.x()+lineOffset.x(),blockOffset.y()+lineOffset.y()); QPoint lineEndPoint=QPoint(linePoint.x()+PAGEWIDTH,linePoint.y()); // cout<<"block:"<<rect.left()<<","<<rect.right()<<":"<<rect.top()<<","<<rect.bottom(); // cout<<"blockstart:"<<blockStart; // int x=blockPoint.x()+(float)((selectionStart-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH; // int y=blockPoint.y()+((selectionStart-blockStart)/LINELENGTH+1)*LINEHEIGHT; // int x1=blockPoint.x()+(float)((selectionEnd-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH; // cout<<"block position:"<<blockPoint.x()<<","<<blockPoint.y(); // cout<<"selection position:"<<x<<","<<y<<":"<<x1<<","<<y; //// int y1=blockPoint.y()+((cursor.selectionEnd()-blockStart)/LINELENGTH+1)*15; // QPoint selectionPoint(x,y); // QPoint selectionEndPoint(x1,y); image->paintLine(linePoint,lineEndPoint); // int lineStart=blockStart,lineEnd; // for(int i=0;i<block.lineCount();i++){ // QTextLine line = layout->lineAt(i); // qreal lineWidth=line.width(),lineHeight=line.height(); // lineEnd=lineStart+line.textLength(); // int a=line.textStart(),b=line.textLength()+a; // cout<<"line.cursorToX:"<<line.cursorToX(selectionStart)<<","<<line.cursorToX(selectionEnd); // cout<<"line.textstart:"<<a<<","<<b; // cout<<"line.width:"<<lineWidth<<" height:"<<lineHeight; // rect=line.naturalTextRect(); // cout<<"line.naturaltextrect:"<<rect.left()<<":"<<rect.top()<<":"<<rect.right()<<":"<<rect.bottom(); // lineStart=lineEnd; // } }
bool Line::lineCircle(float x1, float y1, float x2, float y2, float x_, float y_, float r_) { // is either end INSIDE the circle? // if so, return true immediately bool inside1 = pointCircle(x1, y1, x_, y_, r_); bool inside2 = pointCircle(x2, y2, x_, y_, r_); if (inside1 || inside2) return true; // get length of the line distX = x1 - x2; distY = y1 - y2; len = sqrt((distX*distX) + (distY*distY)); // get dot product of the line and circle dot = (((x_ - x1)*(x2 - x1)) + ((y_ - y1)*(y2 - y1))) / pow(len, 2); // find the closest point on the line closestX = x1 + (dot * (x2 - x1)); closestY = y1 + (dot * (y2 - y1)); // is this point actually on the line segment? // if so keep going, but if not, return false bool onSegment = linePoint(x1, y1, x2, y2, closestX, closestY); if (!onSegment) return false; // optionally, draw a circle at the closest // point on the line ofColor(90, 90, 0); ofEllipse(closestX, closestY, 20, 20); // get distance to closest point distX = closestX - x_; distY = closestY - y_; distance = sqrt((distX*distX) + (distY*distY)); if (distance <= r_) { return true; } else { return false; } }
/** * Updates the preview points (if necessary) * */ void EFX::updatePreview() { if (m_previewPointArray == NULL) return; int stepCount = 128; int step = 0; float stepSize = (float)(1) / ((float)(stepCount) / (M_PI * 2.0)); float i = 0; float *x = new float; float *y = new float; /* Resize the array to contain stepCount points */ m_previewPointArray->resize(stepCount); if (m_algorithm == KCircleAlgorithmName) { /* Draw a preview of a circle */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { circlePoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else if (m_algorithm == KEightAlgorithmName) { /* Draw a preview of a eight */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { eightPoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else if (m_algorithm == KLineAlgorithmName) { /* Draw a preview of a line */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { linePoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else if (m_algorithm == KDiamondAlgorithmName) { /* Draw a preview of a diamond */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { diamondPoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else if (m_algorithm == KTriangleAlgorithmName) { /* Draw a preview of a triangle */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { trianglePoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else if (m_algorithm == KLissajousAlgorithmName) { /* Draw a preview of a lissajous */ for (i = 0; i < (M_PI * 2.0); i += stepSize) { lissajousPoint(this, i, x, y); m_previewPointArray->setPoint(step++, static_cast<int> (*x), static_cast<int> (*y)); } } else { m_previewPointArray->resize(0); } delete x; delete y; }
bool EFX::preview(QVector <QPoint>& polygon) { bool retval = true; int stepCount = 128; int step = 0; qreal stepSize = (qreal)(1) / ((qreal)(stepCount) / (M_PI * 2.0)); qreal i = 0; qreal x = 0; qreal y = 0; /* Resize the array to contain stepCount points */ polygon.resize(stepCount); /* Since algorithm is identified by a string, we don't want to do N string comparisons on each for loop increment. So, it's a bit faster to check the algorithm only once and then do the looping. */ if (m_algorithm == KCircleAlgorithmName) { /* Draw a preview of a circle */ for (step = 0; step < stepCount; step++) { circlePoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else if (m_algorithm == KEightAlgorithmName) { /* Draw a preview of a eight */ for (step = 0; step < stepCount; step++) { eightPoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else if (m_algorithm == KLineAlgorithmName) { /* Draw a preview of a line */ for (step = 0; step < stepCount; step++) { linePoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else if (m_algorithm == KDiamondAlgorithmName) { /* Draw a preview of a diamond */ for (step = 0; step < stepCount; step++) { diamondPoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else if (m_algorithm == KTriangleAlgorithmName) { /* Draw a preview of a triangle */ for (step = 0; step < stepCount; step++) { trianglePoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else if (m_algorithm == KLissajousAlgorithmName) { /* Draw a preview of a lissajous */ for (step = 0; step < stepCount; step++) { lissajousPoint(this, i, &x, &y); polygon[step] = QPoint(int(x), int(y)); i += stepSize; } } else { polygon.resize(0); retval = false; } return retval; }