基於matlab mex的平面點集按重心逆時針排序演算法,matlabmex

來源:互聯網
上載者:User

基於matlab mex的平面點集按重心逆時針排序演算法,matlabmex

基於matlab mex的平面點集按重心逆時針排序演算法,可用於求凸集,代碼如下:

#include <mex.h>#include <mat.h>#include <stdlib.h>#include <vector>#include <algorithm>using namespace std;typedef struct PointF {    int id;    double x, y;    PointF() {        id = -1;    }    PointF(int id, double x, double y) : id(id), x(x), y(y) {}} PointF;typedef vector<PointF> Points;Points points;PointF cpt;bool compare(const PointF p1, const PointF p2) {    //mexPrintf("%d %d\n", p1.id, p2.id);    PointF a = p1, b = p2;    int aid, bid;    if (a.x >= cpt.x) {        if (a.y <= cpt.y)            aid = 1;        else            aid = 2;    }    if (a.x < cpt.x) {        if (a.y >= cpt.y)            aid = 3;        else            aid = 4;    }    if (b.x >= cpt.x) {        if (b.y <= cpt.y)            bid = 1;        else            bid = 2;    }    if (b.x < cpt.x) {        if (b.y >= cpt.y)            bid = 3;        else            bid = 4;    }    if (aid < bid)        return true;    else if (aid == bid) {        if (aid == 1 || aid == 2)            return a.y < b.y;        if (aid == 3 || aid == 4)            return a.y > b.y;    }    else        return false;}void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {    if (nrhs != 2) {        mexPrintf("Usage: 2dsort(xy_nrows_2cols, refpoint_x_y).\n");        return;    }        int ndim = mxGetNumberOfDimensions(prhs[0]);    //mexPrintf("%d\n", ndim);    const int *dims = mxGetDimensions( prhs[0] );    //mexPrintf("%d %d dims\n", dims[0], dims[1]);    if (dims[1] != 2) {       mexPrintf("Error: input data should be with 2 columns.\n");       return;    }        double *data = mxGetPr( prhs[0] );    for(int i = 0; i < dims[0]; i++) {        points.push_back(PointF(i + 1, data[i], data[i + dims[0]]));        //mexPrintf("*%d ", points[i].id);    }        double *data1 = mxGetPr( prhs[1] );    cpt.x = data1[0];    cpt.y = data1[1];    for(int i = 0; i < dims[0]; i++) {        mexPrintf("%d ", points[i].id);    }    mexPrintf("\n");    sort(points.begin(), points.end(), compare);    for(int i = 0; i < dims[0]; i++) {        mexPrintf("%d ", points[i].id);    }    mexPrintf("\n");        nlhs = 1;    plhs[0] = mxCreateDoubleMatrix(dims[0], 1, mxREAL);    double *dest = mxGetPr( plhs[0] );    for(int i = 0; i < dims[0]; i++)        dest[i] = points[i].id;}

mex twodsort.cpp


測試代碼:

close all;mex twodsort.cpp;N = 300;xy = rand(N, 2);ctr = sum(xy) ./ N;plot(xy(:, 1), xy(:, 2), '*r');hold on;plot(ctr(1), ctr(2), '+b');ids = twodsort(xy, ctr);ids(end + 1) = ids(1);plot(xy(ids(1), 1), xy(ids(1), 2), '.g', 'MarkerSize', 5);for i = 1:N    j = i + 1;    if (j > N)        j = 1;    end;    plot([xy(ids(i), 1); xy(ids(j), 1)], [xy(ids(i), 2); xy(ids(j), 2)], '-', 'Color', [i/(N+1), 0, 0], 'LineWidth', 2);end;


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.