|
[code]//////////////////////////////////////MyGroupMyTest2/////////////////////////////////////////////
struct quad{
int X;
int L1;
int L2;
};
static vector<quad> randomQuad(int n, int low, int up) {
vector<quad> ans;
quad g;
g.X=0;
g.L1=1436;
g.L2=1436;
ans.push_back(g);
/*ans.push_back(quad{0,1436,1436});*/
for (int i = 0; i < n; ++i) {
g.X=(rand() % (up - low));
g.L1=(rand() % (up - low) + low);
g.L2=(rand() % (up - low) + low);
ans.push_back(g);
/*acutPrintf(_T("\n%d - %d, %d, %d"),i, g.X,g.L1,g.L2);*/
/*ans.push_back(quad{ (rand() % (up - low)), (rand() % (up - low) + low), (rand() % (up - low) + low) });*/
}
return ans;
}
static void flip_horizontal(quad &q){
int temp;
q.X = -q.X;
temp = q.L1;
q.L1 = q.L2;
q.L2 = temp;
}
static void flip_vertical(quad& q) {
q.X = q.L1 - q.L2 - q.X;
}
static int offset(quad a, quad b, int gap) {
int Offset = 0;
int D1 = a.L1 - (a.L2 + a.X);
int D2 = -b.X;
(D1 - D2 > 0) ? Offset = a.L1 + gap : Offset = a.X + a.L2 - b.X + gap;
return Offset;
}
static int rightMost(quad a, quad b, int gap) {
int Offset = 0;
int rightMost = 0;
int D1 = a.L1 - (a.L2 + a.X);
int D2 = -b.X;
(D1 - D2 > 0) ? Offset = a.L1 + gap : Offset = a.X + a.L2 - b.X + gap;
(b.L1 > b.X + b.L2) ? rightMost = Offset + b.L1 : rightMost = Offset + b.X + b.L2;
(a.L1 > a.X + a.L2) ? rightMost -= a.L1 : rightMost -= (a.X + a.L2);
return rightMost;
}
static int partition(vector<quad>& vec, int start, int end) {
int pivot = end;
int j = start;
for (int i = start; i < end; ++i) {
if ((vec[i].L1 + vec[i].L2) < (vec[pivot].L1 + vec[pivot].L2)) {
swap(vec[i], vec[j]);
++j;
}
}
swap(vec[j], vec[pivot]);
return j;
}
static void quickSort(vector<quad>& vec, int start, int end) {
if (start < end) {
int p = partition(vec, start, end);
quickSort(vec, start, p - 1);
quickSort(vec, p + 1, end);
}
}
static vector<vector<quad>> fit(vector<quad> vec, int chunk, int gap) {
quickSort(vec, 0, (INT)vec.size() - 1);
vector<vector<quad>> ans;
int total = 0;
quad a = {0,0,0};
quad b;
vector<quad>::iterator e;
for (int size = (INT)vec.size(); size > 0; size = (INT)vec.size()) {
vector<quad> temp;
total = 0;
//a = { 0,0,0 };
a.X=0; a.L1=0;a.L2=0;
e = closestSlope(vec, a);
b = (*e);
for (; size > 0 && (total + rightMost(a, b, gap)) <= chunk; size =(int) vec.size()) {
total += rightMost(a, b, gap);
temp.push_back(b);
vec.erase(e);
if (vec.end() == vec.begin()) { break; }
a = b;
e = closestSlope(vec, a);
b = (*e);
}
for (int index = nextSmallest(vec, a, (chunk - total)); size > 0 && index != -1; size = (int)vec.size(), index = nextSmallest(vec, a, (chunk - total))) {
b = vec[index];
total += rightMost(a, b, gap);
temp.push_back(vec[index]);
vec.erase(vec.begin() + index);
a = b;
e = closestSlope(vec, a);
}
ans.push_back(temp);
}
return ans;
}
static int binarySearch(vector<quad>& vec, int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (2*x == vec[mid].L1 + vec[mid].L2)
return mid;
else if (mid == 0)
return -1;
else if (2 * x < vec[mid].L1 + vec[mid].L2)
return binarySearch(vec, l, mid - 1, x);
else
return binarySearch(vec, mid + 1, r, x);
}
return r;
}
static int nextSmallest(vector<quad>& vec, quad a , int total) {
int ans;
quad b;
ans = binarySearch(vec, 0, (int)vec.size() - 1, total);
while (ans >= 0) {
b = vec[ans];
if ((total - rightMost(a, vec[ans], 0)) > 0) { break; }
--ans;
}
return ans;
}
static vector<quad>::iterator closestSlope(vector<quad>& vec, quad a) {
int D1 = a.L1 - (a.L2 + a.X);
int D2 , temp;
int lowest = INT_MAX;
vector<quad>::iterator ans;
/*quad b;*/
for (vector<quad>::iterator e = (vec.end() - 1); vec.size()>0 ;--e) {
quad b = (*e);
D2 = -b.X;
temp = abs(D1 - D2);
if (temp == 0) { lowest = temp; return e; }
if (temp < lowest) { lowest = temp; ans = e; }
if (e == vec.begin()) { break; }
}
return ans;
}
static void Draw_quad (vector<quad>& vec) {
for (int i=0 ; i < (int)vec.size(); ++i) {
acutPrintf(_T("\nvec= %d - %d - %d - %d"),i,vec.at(i).X,vec.at(i).L1,vec.at(i).L2);
AcGePoint2d ptLeftBottom(vec.at(i).X, 0);
AcGePoint2d ptRightBottom(vec.at(i).X+vec.at(i).L1, 0);
AcGePoint2d ptRightTop(vec.at(i).L2, 100);
AcGePoint2d ptLeftTop(0, 100);
AcGePoint2dArray quadpts;
quadpts.append(ptLeftBottom);
quadpts.append(ptRightBottom);
quadpts.append(ptRightTop);
quadpts.append(ptLeftTop);
Create2dPolyline(quadpts,0,i);
}
}
static AcDbObjectId Create2dPolyline(const AcGePoint2dArray &points,double width,int nNewColor)
{
int numVertices=points.length();
AcDbPolyline *pPoly=new AcDbPolyline(numVertices);
for (int i=0;i<numVertices;i++)
{
pPoly->addVertexAt(i,points.at(i),0,width,width);
}
pPoly->setColorIndex(nNewColor);
pPoly->setClosed(Adesk::kTrue);
pPoly->setLayer(_T("sta"));
return PostToModelSpace(pPoly);
}
static void MyGroupMyCommand2 ()
{
/* Mat drawing;*/
vector<quad> vec;
vec= randomQuad(10, 30, 90); //180 代表180个 L1 0~90 L2 0~90 X 0~90
vector<vector<quad>> yyy = fit(vec,1440,4);
Draw_quad(vec);
for (int i=0 ; i < (int)yyy.size(); ++i) {
for (int ii=0 ; ii < (int)yyy.at(i).size(); ++ii) {
double width = 0;
// 计算矩形的角点
AcGePoint2d ptLeftBottom(vec.at(ii).X, 200);
AcGePoint2d ptRightBottom(vec.at(ii).X+vec.at(ii).L1,200);
AcGePoint2d ptRightTop(vec.at(ii).L2, 300);
AcGePoint2d ptLeftTop(0, 300);
// 创建对应的多段线
AcDbPolyline* pPoly = new AcDbPolyline(4);
pPoly->addVertexAt(0, ptLeftBottom, 0, width, width);
pPoly->addVertexAt(1, ptRightBottom, 0, width, width);
pPoly->addVertexAt(2, ptRightTop, 0, width, width);
pPoly->addVertexAt(3, ptLeftTop, 0, width, width);
pPoly->setClosed(Adesk::kTrue);
pPoly->setColorIndex(ii);
pPoly->setLayer(_T("end"));
// 将多段线添加到模型空间
AcDbObjectId polyId;
polyId = PostToModelSpace(pPoly);
}
}
}
//////////////////////////////////////MyGroupMyTest2/////////////////////////////////////////////[/code] |
|