How to combine selected points to one averaged point?

Use this forum for all posts and questions about the free QCAD Community Edition version 3

Moderator: andrew

Post Reply
Dauu
Newbie Member
Posts: 5
Joined: Wed Jun 23, 2021 4:58 pm

How to combine selected points to one averaged point?

Post by Dauu » Sat Jul 31, 2021 1:16 am

How to combine selected points to their average point?
Removing all the selected points and replace them with their average point (in xy QCAD or in xyz QCAM)
.
av.png
av.png (55.55 KiB) Viewed 6129 times

CVH
Premier Member
Posts: 3364
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to combine selected points to one averaged point?

Post by CVH » Sat Jul 31, 2021 2:30 am

Hi,
remark here that QCAD/CAM is 2D native and only handles z components up to a point.

There a many strategies for that.
In 2D simply the center of the best fitted circle ...
One could take the mean of the first coordinates and the mean of the second coordinates.
or one could take the mean of the magnitudes and the mean of directions of the vectors.

Other strategies may exclude inner points ... exorbitant positions ...
... or the centroid of the hull shape/body ...

All depends. :wink:

Collecting data would be done by a script that applies the required calculations.

In your example the selected dots would represent a lot of noise in that area.
I see a mean pathway from left to right using 3 or 4 positions instead of 1 single mean value ...

Regards,
CVH

CVH
Premier Member
Posts: 3364
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to combine selected points to one averaged point?

Post by CVH » Sat Jul 31, 2021 5:37 am

A rather simplistic script for average and mean position ...
It displays the results textual in the Command History and adds both points to the document.

Please:

Code: Select all

include("scripts/simple.js");
var doc = this.getDocument();
var xValues = [];
var yValues = [];
var zValues = [];

var ids = doc.querySelectedEntities();
for (var i=0;i<ids.length;i++){
    var entity = doc.queryEntityDirect(ids[i]);
    if (isPointEntity(entity)){
        xValues.push(entity.getPosition().x);
        yValues.push(entity.getPosition().y);
        zValues.push(entity.getPosition().z);
    }
}

var xAve, yAve, zAve;
var xMean, yMean, zMean;
xAve = yAve = zAve = xMean = yMean = zMean = 0.0;
if (xValues.length > 1) {
    for (var i=0;i<xValues.length;i++){
        xAve += xValues[i]/xValues.length;
        yAve += yValues[i]/xValues.length;
        zAve += zValues[i]/xValues.length;

        xMean += Math.sign(xValues[i])*xValues[i]*xValues[i]/xValues.length;
        yMean += Math.sign(yValues[i])*yValues[i]*yValues[i]/xValues.length;
        zMean += Math.sign(zValues[i])*zValues[i]*zValues[i]/xValues.length;
    }

    var avePos = new RVector(xAve, yAve, zAve);

    xMean = Math.sign(xMean)*sqrt(Math.abs(xMean));
    yMean = Math.sign(yMean)*sqrt(Math.abs(yMean));
    zMean = Math.sign(zMean)*sqrt(Math.abs(zMean));
    var meanPos = new RVector(xMean, yMean, zMean);

    addPoint(avePos);
    EAction.handleUserMessage("Average position: " + avePos);
    addPoint(meanPos);
    EAction.handleUserMessage("Mean position: " + meanPos);
}
else {
    EAction.handleUserWarning("No multiple points selected.");
}
Regards,
CVH

Dauu
Newbie Member
Posts: 5
Joined: Wed Jun 23, 2021 4:58 pm

Re: How to combine selected points to one averaged point?

Post by Dauu » Sun Aug 01, 2021 4:26 pm

CVH wrote:
Sat Jul 31, 2021 5:37 am
A rather simplistic script for average and mean position ...
It displays the results textual in the Command History and adds both points to the document
Thank you, this did the job

Post Reply

Return to “QCAD Community Edition”