代码拉取完成,页面将自动刷新
同步操作将从 harryzhang/XY串联型机械臂算法实现 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
static TCHAR szAppName[] = TEXT("GDI Test");
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hWnd = CreateWindow(szAppName, // window class name
szAppName, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
1000, // initial x size
500, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//绘制指定属性的直线
static void DrawLine(HDC hDC, int x0, int y0, int x1, int y1, int style, int width, COLORREF color)
{
HPEN hPen = CreatePen(style, width, color);
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
MoveToEx(hDC, x0, y0, NULL);
LineTo(hDC, x1, y1);
SelectObject(hDC, hOldPen);
DeleteObject(hPen);
}
//绘制实心圆
static void DrawCircle(HDC hDC, int x, int y, int len, COLORREF color)
{
HBRUSH hBrush = CreateSolidBrush(color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
HPEN hPen = CreatePen(PS_SOLID, 1, color);
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
Ellipse(hDC, x-len/2, y-len/2, x+len/2, y+len/2);
SelectObject(hDC, hOldBrush);
DeleteObject(hPen);
SelectObject(hDC, hOldPen);
DeleteObject(hOldBrush);
}
//绘制填充矩形
static void DrawRect(HDC hDC, int left, int top, int width, int height, int style, COLORREF color)
{
HBRUSH hBrush = CreateHatchBrush(style, color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
Rectangle(hDC, left, top, left+width, top+height);
SelectObject(hDC, hOldBrush);
DeleteObject(hOldBrush);
}
//绘制位图填充矩形
static void DrawBmpRect(HDC hDC, int left, int top, int width, int height, LPCTSTR file)
{
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
HBRUSH hBrush = CreatePatternBrush(hBitmap);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
Rectangle(hDC, left, top, left+width, top+height);
SelectObject(hDC, hOldBrush);
DeleteObject(hOldBrush);
DeleteObject(hBitmap);
}
const float L1 = 110.0;
const float L2 = 110.0;
const float H = 110.0;
static char buf[200];
typedef struct point{
float x;
float y;
};
typedef struct solution{
point inv;
point solved;
float ORG_X;
float ORG_Y;
float CACL_A;
float CACL_B;
float DeltaX;
bool success;
};
static int getrealx(int x)
{
return x+10;
}
static int getrealy(int y)
{
if(y<0) y=0;
if(y>400-20) y = 400-20;
return 400 - 20 - y;
}
//ax^2+bx+c=0,返回较小方程的根
static solution caclduo(float a,float b,float c)
{
solution solu;
float root = pow(b,2)-4*a*c;
solu.success = true;
if(root>=0 || a!=0)
{
if(root == 0)
{
solu.DeltaX = -b/2*a;
solu.success = true;
goto end;
}
float x1 = (-b+sqrt(root))/(2*a);
float x2 = (-b-sqrt(root))/(2*a);
// cout << "a:" << a << " b:" << b << " c:" << c << " root:" << root << " Dx1:" << x1 << " Dx2:" << x2 << endl;
if(x1>x2)
solu.DeltaX = x2;
else
solu.DeltaX = x1;
}
else
solu.success = false;
end:return solu;
}
static solution invx(solution solu2)
{
solution solu = solu2;
float L1_U,L2_U;
if(solu.CACL_A > solu.CACL_B){
L1_U = L1;
L2_U = L2;
}else{
L1_U = L2;
L2_U = L1;
}
float ax = solu.CACL_A-solu.CACL_B;
float A = pow(H,2)+pow(L1_U,2)-pow(ax,2)-pow(L2_U,2);
float B = 2*H*L1_U;
float C = 2*ax*L2_U;
float a = pow(B,2)*pow(L2_U,2)+pow(C,2)*pow(L1_U,2);
float b = -2*L1_U*A*B*pow(L2_U,2)-2*H*pow(L1_U,2)*pow(C,2);
float c = pow(A,2)*pow(L1_U,2)*pow(L2_U,2)-pow(C,2)*pow(L1_U,2)*pow(L2_U,2)+pow(C,2)*pow(L1_U,2)*pow(H,2);
solution solu3 = caclduo(a,b,c);
if(solu.CACL_A > solu.CACL_B){
solu.solved.x = solu.CACL_B + sqrt(pow(L1,2)-pow(solu3.DeltaX,2));
solu.solved.y = solu3.DeltaX;
}else{
solu.solved.x = solu.CACL_A + sqrt(pow(L2,2)-pow(solu3.DeltaX,2));
solu.solved.y = H-solu3.DeltaX;
}
// cout << sqrt(pow(L2,2)-pow(solu.DeltaX,2)) << "," << sqrt(pow(L1,2)-pow(solu.DeltaX,2)) << "," << org_a << "," << org_b << "," << ax << endl;
return solu;
}
static solution getx(float x,float y)
{
solution solu = caclduo(1,-2*x,pow(x,2)+pow(y,2)-pow(L1,2));
solu.inv.x = x;
solu.inv.y = y;
if(solu.success)
{
x -= solu.DeltaX;
float a1 = atan(y/x);
float sina2 = (H-L1*sin(a1))/L2;
float cosa2 = sqrt(1-pow(sina2,2));
float ax = L2*cosa2;
// cout << "a1:" << a1 << " sina1:" << sin(a1) << " sina2:" << sina2 << " cosa2:" << cosa2 << " ax:" << ax <<" DeltaX:" << solu.DeltaX << endl;
solu.CACL_A = solu.DeltaX + (x-ax);
solu.CACL_B = solu.DeltaX;
}
return solu;
}
static float getdist(float x0,float y0,float x1,float y1)
{
return sqrt(pow(x1-x0,2)+pow(y1-y0,2));
}
#define Ax 250
#define Ay 20
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
solution solu,solu2;
solu = getx(Ax,Ay);
solu2 = invx(solu);
switch (message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
{
hDC = BeginPaint(hWnd, &ps);
DrawLine(hDC, getrealx(0), getrealy(0), getrealx(0), getrealy((int)H), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(0), getrealy((int)H), getrealx(800), getrealy((int)H), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(800), getrealy((int)H), getrealx(800), getrealy(0), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(0), getrealy(0), getrealx(800), getrealy(0), PS_SOLID, 2, RGB(0,255,0));
if(!solu.success) TextOut(hDC, 0, 0, TEXT("坐标错误"), 10);
else{
sprintf(buf,"通过H、L点正解A点:%.01lf,%.01lf",solu2.solved.x,solu2.solved.y);
TextOut(hDC, 0, 80, TEXT(buf), strlen(buf));
sprintf(buf,"H(%.01lf,%.01lf)",solu.CACL_A,H);
TextOut(hDC, getrealx((int)solu.CACL_A), getrealy((int)H+20), TEXT(buf), strlen(buf));
sprintf(buf,"L(%.01lf,%.01lf)",solu.CACL_B,0.0);
TextOut(hDC, getrealx((int)solu.CACL_B), getrealy(0), TEXT(buf), strlen(buf));
sprintf(buf,"A(%d,%d)",Ax,Ay);
TextOut(hDC, getrealx(Ax+1), getrealy(Ay), TEXT(buf), strlen(buf));
sprintf(buf,"DeltaX:%.01lf",solu.DeltaX);
TextOut(hDC, 0, 60, TEXT(buf), strlen(buf));
sprintf(buf,"L2:%.01lf",getdist(Ax,Ay,solu.CACL_A,H));
TextOut(hDC, getrealx((int)(Ax+solu.CACL_A)/2), getrealy((H+Ay)/2+10), TEXT(buf), strlen(buf));
sprintf(buf,"L1:%.01lf",getdist(Ax,Ay,solu.CACL_B,0));
TextOut(hDC, getrealx((int)(Ax+solu.CACL_B)/2), getrealy((Ay)/2), TEXT(buf), strlen(buf));
DrawCircle(hDC, getrealx((int)solu.CACL_A), getrealy((int)H), 10, RGB(0, 0, 255));
DrawCircle(hDC, getrealx((int)solu.CACL_B), getrealy(0), 10, RGB(0, 0, 255));
DrawLine(hDC, getrealx((int)solu.CACL_A), getrealy((int)H),getrealx(Ax), getrealy(Ay), PS_SOLID, 2, RGB(0,0,255));
DrawLine(hDC, getrealx((int)solu.CACL_B), getrealy(0), getrealx(Ax), getrealy(Ay),PS_SOLID, 2, RGB(0,0,255));
DrawCircle(hDC, getrealx(Ax), getrealy(Ay), 10, RGB(255, 0, 0));
}
}
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。