📄 gridsub2.c
字号:
xloc += reldisp.x;
yloc += reldisp.y;
generatenodes();
}
// return YES if the block is completely inside the box
int block::inbox(dRECT drect){
int i;
dPOINT p;
for(i = 1; i <= nvertex(); i++){
vertex(&p, i);
if((p.x > drect.right) || (p.x < drect.left) ||
(p.y > drect.top ) || (p.y < drect.bottom)) return NO;
}
return YES;
}
// return YES if the block is at least partially inside the
// view window.
int block::outsidewindow(void){
int i, n, sureoutside;
dPOINT p[MAXCORNERS+1];
n = nvertex();
for(i = 1; i <= n; i++)
vertex(&p[i], i);
sureoutside = YES;
if(p[1].x < winleft){
for(i = 2; i <= n; i++){
if(p[i].x > winleft) sureoutside = NO;
}
if(sureoutside) return YES;
}
sureoutside = YES;
if(p[1].x > winright){
for(i = 2; i <= n; i++){
if(p[i].x < winright) sureoutside = NO;
}
if(sureoutside) return YES;
}
sureoutside = YES;
if(p[1].y < winbottom){
for(i = 2; i <= n; i++){
if(p[i].y > winbottom) sureoutside = NO;
}
if(sureoutside) return YES;
}
sureoutside = YES;
if(p[1].y > wintop){
for(i = 2; i <= n; i++){
if(p[i].y < wintop) sureoutside = NO;
}
if(sureoutside) return YES;
}
return NO;
}
block * block::enclosepoint(dPOINT dpoint){
int i, nv;
double x1, y1, x2, y2;
dPOINT previousp, currentp;
nv = nvertex();
vertex(&previousp, nv);
for(i = 1; i <= nv; i++){
vertex(¤tp, i);
x1 = currentp.x - previousp.x;
y1 = currentp.y - previousp.y;
x2 = dpoint.x - previousp.x;
y2 = dpoint.y - previousp.y;
if(crossproduct(x1, y1, x2, y2) < 0.0) return NULL;
previousp = currentp;
}
return this;
}
//
// 2-------1
// | |
// | |
// 3-------4
block* block::hook(dPOINT hookpoint, int *hookcorner){
int i, nv;
dPOINT v, delrec;
nv = nvertex();
pixelresolution(&delrec);
for(i = 1; i <= nv; i++){
vertex(&v, i);
if((hookpoint.x < v.x + HOOKTOLERANCE * delrec.x) &&
(hookpoint.x > v.x - HOOKTOLERANCE * delrec.x) &&
(hookpoint.y < v.y + HOOKTOLERANCE * delrec.y) &&
(hookpoint.y > v.y - HOOKTOLERANCE * delrec.y)){
*hookcorner = i;
return this;
}
}
*hookcorner = 0;
return NULL;
}
void block::drawsidesandnodes(HDC hdc){
int i, t;
double xs, ys, xe, ye;
for(i = 0; i < (t = exteriorelmtcount()); i++){
exteriornode[i].draw(hdc);
}
xs = exteriornode[0].x;
ys = exteriornode[0].y;
for(i = 0; i < exteriorelmtcount(); i++){
xe = exteriornode[(i+1)%t].x;
ye = exteriornode[(i+1)%t].y;
exteriorside[i].draw(hdc, xs, ys, xe, ye);
xs = xe;
ys = ye;
}
}
void block::clearnumber(void){
int i;
for(i = 0; i < exteriorelmtcount(); i++){
exteriornode[i].index = 0;
exteriorside[i].index = 0;
}
}
COUNT block::numberexteriorelmts(COUNT count){
int i, k;
ID tmpid;
COUNT n;
block *destcell;
n = count;
for(i = 0; i < exteriorelmtcount(); i++){
if(exteriornode[i].index == 0){
exteriornode[i].index = n.node;
// update connected nodes
destcell = this;
k = i;
while((tmpid = destcell->exteriornode[k].cellid) != id){
k = destcell->exteriornode[k].node;
if((destcell = unselected.by_id(tmpid)) == NULL)
if((destcell = selected.by_id(tmpid)) == NULL)
MessageBeep(0);
destcell->exteriornode[k].index = n.node;
}
(n.node)++;
}
if(exteriorside[i].index == 0){
exteriorside[i].index = n.side;
// update connected sides
destcell = this;
k = i;
while((tmpid = destcell->exteriorside[k].cellid) != id){
k = destcell->exteriorside[k].side;
if((destcell = unselected.by_id(tmpid)) == NULL)
if((destcell = selected.by_id(tmpid)) == NULL)
MessageBeep(0);
destcell->exteriorside[k].index = n.side;
}
(n.side)++;
}
}
return n;
}
COUNT block::genexteriornodeinfo(FILE *stream, COUNT count){
unsigned int i, n;
COUNT c;
n = count.node;
for(i = 0; i < exteriorelmtcount(); i++){
if(exteriornode[i].index == n){
fprintf(stream, "%u %u %lg %lg\n", n,
exteriornode[i].property & PROPERTYMASK,
exteriornode[i].x, exteriornode[i].y);
n++;
}
}
c.node = n;
c.side = count.side;
return c;
}
COUNT block::genexteriorsideinfo(FILE *stream, COUNT count){
unsigned int i, n, s, extelm;
COUNT c;
n = count.node;
s = count.side;
for(i = 0; i < (extelm = exteriorelmtcount()); i++){
if(exteriornode[i].index == n) n++;
if(exteriorside[i].index == s){
fprintf(stream, "%u %u %u %u\n",
s, exteriorside[i].property & PROPERTYMASK,
exteriornode[i].index, exteriornode[(i+1) % extelm].index);
s++;
}
}
c.node = n;
c.side = s;
return c;
}
void block::getsize(dRECT *ps){
int i;
dPOINT p;
for(i = 1; i <= nvertex(); i++){
vertex(&p, i);
if(p.x > ps->right){
ps->right = p.x;
}
if(p.x < ps->left){
ps->left = p.x;
}
if(p.y > ps->top){
ps->top = p.y;
}
if(p.y < ps->bottom){
ps->bottom = p.y;
}
}
}
int block::withinboundary(dPOINT reldisp){
int i;
dPOINT p;
for(i = 1; i <= nvertex(); i++){
vertex(&p, i);
if(p.x + reldisp.x > paperxcenter + deskwidth/2.0) return NO;
if(p.x + reldisp.x < paperxcenter - deskwidth/2.0) return NO;
if(p.y + reldisp.y > paperycenter + deskheight/2.0) return NO;
if(p.y + reldisp.y < paperycenter - deskheight/2.0) return NO;
}
return YES;
}
int block::withinboundary(double x, double y){
if(x > paperxcenter + deskwidth/2.0) return NO;
if(x < paperxcenter - deskwidth/2.0) return NO;
if(y > paperycenter + deskheight/2.0) return NO;
if(y < paperycenter - deskheight/2.0) return NO;
return YES;
}
int block::withinboundary(dPOINT pivot, double sinangle, double cosangle){
int i;
dPOINT p, arm;
for(i = 1; i <= nvertex(); i++){
vertex(&p, i);
arm.x = p.x - pivot.x;
arm.y = p.y - pivot.y;
rotatepoint(&arm, sinangle, cosangle);
p.x = pivot.x + arm.x;
p.y = pivot.y + arm.y;
if(p.x > paperxcenter + deskwidth/2.0) return NO;
if(p.x < paperxcenter - deskwidth/2.0) return NO;
if(p.y > paperycenter + deskheight/2.0) return NO;
if(p.y < paperycenter - deskheight/2.0) return NO;
}
return YES;
}
int block::withinboundary(dPOINT loc, double factor){
int i;
dPOINT p, p0, ref;
getreference(&ref);
for(i = 1; i <= nvertex(); i++){
vertex(&p0, i);
p.x = factor * (p0.x - ref.x);
p.y = factor * (p0.y - ref.y);
if(p.x + loc.x > paperxcenter + deskwidth/2.0) return NO;
if(p.x + loc.x < paperxcenter - deskwidth/2.0) return NO;
if(p.y + loc.y > paperycenter + deskheight/2.0) return NO;
if(p.y + loc.y < paperycenter - deskheight/2.0) return NO;
}
return YES;
}
void block::assignid(void){
ID newid;
newid = getnextavailableid(1);
setid(newid);
}
static ID nextavailableid = 1;
void resetnextavailableid(void){
nextavailableid = 1;
}
ID block::getnextavailableid(ID testid){
while(selected.idused(testid) || unselected.idused(testid)){
testid++;
} // this loop will definitely stop because MAXID will never be used.
if(testid == nextavailableid){
nextavailableid++;
if(nextavailableid > MAXID){
MessageBox(hwndPaper, "Too many blocks used", "WARNING",
MB_ICONEXCLAMATION | MB_OK);
blockvacancy = NO;
}
}
return testid;
}
void block::replace(ID originalid, ID newid){
int i;
for(i = 0; i < exteriorelmtcount(); i++){
if((exteriornode[i].cellid == originalid) &&
(!(exteriornode[i].property & CONNECTMASK))){
exteriornode[i].cellid = newid;
exteriornode[i].property |= CONNECTMASK;
}
if((exteriorside[i].cellid == originalid) &&
(!(exteriorside[i].property & CONNECTMASK))){
exteriorside[i].cellid = newid;
exteriorside[i].property |= CONNECTMASK;
}
}
}
/******************************************************************/
/* */
/* Member functions for rectblk */
/* */
/******************************************************************/
// constructor: create objects with initiallization
rectblk::rectblk(double ep, double sgm, double m, double m_sgm,
double x, double y, double l1, double l2,
int d1, int d2, double ang){
epsilon = ep;
sigma = sgm;
mu = m;
sigma_mu = m_sgm;
xloc = x;
yloc = y;
length1 = l1;
length2 = l2;
div1 = d1;
div2 = d2;
angle = ang;
exteriornode = new surnode[exteriorelmtcount()];
exteriorside = new surside[exteriorelmtcount()];
assignid();
generatenodes();
initnodes();
initsides();
}
// Node assignment:
// 12 11 10 9 8
// 13 7
// 14 6
// 15 5
// 0 1 2 3 4
void rectblk::generatenodes(void){
int i;
double sintheta, costheta;
double x1, y1, x2, y2;
dPOINT p, p1, p2, p3, p4;
sintheta = sin(angle * PI / 180.0);
costheta = cos(angle * PI / 180.0);
p.x = length1;
p.y = length2;
rotatepoint(&p, sintheta, costheta);
p1.x = xloc + p.x;
p1.y = yloc + p.y;
p.x = 0.0;
p.y = length2;
rotatepoint(&p, sintheta, costheta);
p2.x = xloc + p.x;
p2.y = yloc + p.y;
p.x = 0.0;
p.y = 0.0;
rotatepoint(&p, sintheta, costheta);
p3.x = xloc + p.x;
p3.y = yloc + p.y;
p.x = length1;
p.y = 0.0;
rotatepoint(&p, sintheta, costheta);
p4.x = xloc + p.x;
p4.y = yloc + p.y;
for(i = 0; i < div1; i++){
p.x = (1.0 - (double)i / div1) * p3.x + ((double)i / div1) * p4.x;
p.y = (1.0 - (double)i / div1) * p3.y + ((double)i / div1) * p4.y;
exteriornode[i].x = p.x;
exteriornode[i].y = p.y;
}
for(i = 0; i < div2; i++){
p.x = (1.0 - (double)i / div2) * p4.x + ((double)i / div2) * p1.x;
p.y = (1.0 - (double)i / div2) * p4.y + ((double)i / div2) * p1.y;
exteriornode[div1 + i].x = p.x;
exteriornode[div1 + i].y = p.y;
}
for(i = 0; i < div1; i++){
p.x = (1.0 - (double)i / div1) * p1.x + ((double)i / div1) * p2.x;
p.y = (1.0 - (double)i / div1) * p1.y + ((double)i / div1) * p2.y;
exteriornode[div1 + div2 + i].x = p.x;
exteriornode[div1 + div2 + i].y = p.y;
}
for(i = 0; i < div2; i++){
p.x = (1.0 - (double)i / div2) * p2.x + ((double)i / div2) * p3.x;
p.y = (1.0 - (double)i / div2) * p2.y + ((double)i / div2) * p3.y;
exteriornode[2 * div1 + div2 + i].x = p.x;
exteriornode[2 * div1 + div2 + i].y = p.y;
}
}
void rectblk::initnodes(void){
int i;
for(i = 0; i < 2 * (div1 + div2); i++){
exteriornode[i].cellid = id;
exteriornode[i].node = i;
}
}
void rectblk::move(dPOINT center, double sintheta, double costheta){
// center is the center at which the object is to be outlined.
// theta is the ADDITIONAL angle to rotate.
// short nDrawingMode = R2_NOTXORPEN;
double sinalpha, cosalpha, sinbeta, cosbeta;
// alpha = beta + theta
sinbeta = sin(angle * PI / 180.0);
cosbeta = cos(angle * PI / 180.0);
sinalpha = sinbeta * costheta + cosbeta * sintheta;
cosalpha = cosbeta * costheta - sinbeta * sintheta;
angle = atan2(sinalpha, cosalpha) * 180.0 / PI;
xloc = center.x;
yloc = center.y;
snaptogrid();
generatenodes();
// initnodes();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -