📄 globe.cpp
字号:
#include "stdafx.h"
#include "Globe.h"
void simul_Reverse(RobotInford *A, RobotInford *B, int Number)
{
int i;
for(i=0;i<(Number - 1)/2;i++)
B[(Number - 1)/2 + i] = A[i];
for(i;i<(Number - 1);i++)
B[i - (Number - 1)/2] = A[i];
B[i] = A[i];
};
void simul_Reverse(RobotInfor *A, RobotInfor *B, int Number)
{
int i;
for(i=0;i<(Number - 1)/2;i++)
B[(Number - 1)/2 + i] = A[i];
for(i;i<(Number - 1);i++)
B[i - (Number - 1)/2] = A[i];
B[i] = A[i];
};
int CVidio::ColorDivision(CWnd *pWnd, COLORREF color, HSV *pHSVREF)
{
RECT rect;
int i, j;
pWnd->GetClientRect(&rect);
CDC *pDc = pWnd->GetDC();
for(i=0; i<=rect.right; i++)
for(j=0; j<=rect.bottom; j++)
{
if(ColorCheck(color, pDc->GetPixel(i,j), pHSVREF))
pDc->SetPixel(i, j, 0x00ffffff - color);
};
return 0;
}
int CVidio::ColorIndex(COLORREF color, RGBQUAD *rgb)
{
int index = 0;
rgb->rgbRed = GetRValue(color);
rgb->rgbReserved = rgb->rgbRed;
rgb->rgbGreen = GetGValue(color);
if(rgb->rgbGreen < rgb->rgbReserved)
{
index = 1;
rgb->rgbReserved = rgb->rgbGreen;
};
rgb->rgbBlue = GetBValue(color);
if(rgb->rgbBlue < rgb->rgbReserved)
{
index = 2;
rgb->rgbReserved = rgb->rgbBlue;
};
return index;
};
int CVidio::GetHSV(COLORREF color, HSV *hsv)//calculate the H, S, V of color
{
RGBQUAD rgb;
int index = this->ColorIndex(color, &rgb);
hsv->V = (rgb.rgbGreen + rgb.rgbBlue + rgb.rgbRed)/3;// V value
if(index == 0)
{
hsv->H = atan2(rgb.rgbGreen - rgb.rgbRed, rgb.rgbBlue - rgb.rgbRed);//H value
hsv->S = rgb.rgbGreen + rgb.rgbBlue;//saturation without the sum!
};
if(index == 1)
{
hsv->H = atan2(rgb.rgbRed - rgb.rgbGreen, rgb.rgbBlue - rgb.rgbGreen);
hsv->S = rgb.rgbRed + rgb.rgbBlue;
};
if(index == 2)
{
hsv->H = atan2(rgb.rgbRed - rgb.rgbBlue, rgb.rgbGreen - rgb.rgbBlue);
hsv->S = rgb.rgbGreen + rgb.rgbRed;
};
return index;
};
int CVidio::ColorCheck(COLORREF colorref, COLORREF color, HSV *colorcrit)
{
RGBQUAD rgb, rgbref;
rgb.rgbRed = GetRValue(color);
rgb.rgbGreen = GetGValue(color);
rgb.rgbBlue = GetBValue(color);
rgbref.rgbRed = GetRValue(colorref);
rgbref.rgbGreen = GetGValue(colorref);
rgbref.rgbBlue = GetBValue(colorref);
HSV hsv, hsvref;
if(GetHSV(color, &hsv) != GetHSV(colorref, &hsvref))
return 0;
hsv.H = fabs(hsv.H - hsvref.H);
///////////************* do not remove
// double a = rgbref.rgbRed+rgbref.rgbGreen+rgbref.rgbBlue;// turn off
// double b = rgb.rgbRed+rgb.rgbGreen+rgb.rgbBlue;// the saturation
// hsv.S = fabs(hsv.S * a - hsvref.S * b);//
hsv.V = fabs(hsv.V - hsvref.V);
if((hsv.H <= fabs(colorcrit->H)) /*&& (hsv.S <= fabs(colorcrit->S * a*b))*/ &&
(hsv.V <= fabs(colorcrit->V)))
return 1;
return 0;
};
int CVidio::NextEdgePt(CDC *pDc, POINT point, POINT *next, COLORREF color, HSV *ref, int start, RECT *pRect)
//point: central point, next:next point; start: start position; pRect: area
{
int i;
if(point.x < pRect->left || point.y < pRect->top ||
point.x > pRect->right || point.y > pRect->bottom)
return -2; //not in the area
for(i=0; i<8; i++) // eight directions
{
if((point.x == pRect->left) && (start==0 || start==7))//bounder process
start = 1;
if((point.y == pRect->top) && (start==1 || start==2))//
start = 3;
if((point.x == pRect->right) && (start==3 || start==4))//
start = 5;
if((point.y == pRect->bottom) && (start==5 || start==6))//
start = 7;
switch (start)
{
case 0:
next->x = point.x - 1; //
next->y = point.y - 1; // 1 2 3
break; // 0 4
case 1: // 7 6 5
next->x = point.x; //
next->y = point.y - 1; //
break;
case 2:
next->x = point.x + 1;
next->y = point.y - 1;
break;
case 3:
next->x = point.x + 1;
next->y = point.y ;
break;
case 4:
next->x = point.x + 1;
next->y = point.y + 1;
break;
case 5:
next->x = point.x ;
next->y = point.y + 1;
break;
case 6:
next->x = point.x - 1;
next->y = point.y + 1;
break;
case 7:
next->x = point.x - 1;
next->y = point.y ;
};
if((start = start + 1) == 8)
start = 0;
if(ColorCheck(color, pDc->GetPixel(*next), ref))
return start;
};
return -1;//do not find
};
int CVidio::GetTarget(CDC *pDc, POINT point, COLORREF colorref, HSV *colorcrit, int pace, RECT *pRect)
{
POINT nextpoint , startpoint, centerpoint;
double centerpointx = 0, centerpointy = 0;
nextpoint.x = -1; nextpoint.y = -1;
int start = 0, i, stpoint, pointsum = 0;
POINT stack[500]; //temp stack
int pointer = 1; //stack pointer
stack[0].x = -1; stack[0].y = -1;
while(ColorCheck(colorref, pDc->GetPixel(point.x-1, point.y), colorcrit))
{
if(point.x <= 0)
break;
point.x = point.x - 1;
};
startpoint = point;
stack[pointer] = startpoint;
while(((start = NextEdgePt(pDc, point, &nextpoint, colorref, colorcrit, start, pRect))
>= 0) && !((startpoint.x==nextpoint.x) && (startpoint.y==nextpoint.y)))
{
POINT pt = nextpoint;
CWnd *pWnd = pDc->GetWindow();
pWnd->ClientToScreen(&pt);
for(i=0; i<=100; i++);
SetCursorPos(pt.x, pt.y);
if((nextpoint.y != stack[pointer - 1].y) && (nextpoint.y != point.y))
{
pointer++;// push the stack
stack[pointer] = nextpoint;
}
else
if(nextpoint.y != point.y)
{
if(stack[pointer].x < point.x)
stpoint = stack[pointer].x;
else
stpoint = point.x;
// center the weight point-----------------------------------------------
i = abs(stack[pointer].x - point.x) + 1;
stpoint = stpoint * i;
stpoint = stpoint + i*(i + 1)/2;
centerpointx = (stpoint + centerpointx * pointsum)
/(i + pointsum);//may take count of the data type!
centerpointy = (point.y * i + centerpointy * pointsum)
/(i + pointsum);//center the weight y
pointsum = i + pointsum;
centerpoint.x = (int)centerpointx;
centerpoint.y = (int)centerpointy;
//-----------------------------------------------------------------------
stack[pointer].x=0; stack[pointer].y=0;
pointer--;// pop the stack
if((pointer == 1) && (point.x != stack[pointer].x))
{
for(i=0;i<abs(stack[pointer].x - point.x);i++)
{
//center the weight
};
pointer--;
};
////////////////////*****************
};
point = nextpoint;
switch(start)
{
case 0:
start = 4;
break;
case 1:
start = 5;
break;
case 2:
start = 6;
break;
case 3:
start = 7;
break;
case 4:
start = 0;
break;
case 5:
start = 1;
break;
case 6:
start = 2;
break;
case 7:
start = 3;
};
}
POINT pt = centerpoint;
CWnd *pWnd = pDc->GetWindow();
pWnd->ClientToScreen(&pt);
SetCursorPos(pt.x, pt.y);
return pointer;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -