Nadesłany przez Krzysztof Zajączkowski, 05 stycznia 2009 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
rzutowanie/rzutowanie/rzutowanie.cpp:
#include <windows.h> #include <math.h> class dPoint2D{ double x; double y; public: dPoint2D(double x,double y); dPoint2D operator -(dPoint2D &p); dPoint2D operator +(dPoint2D &p); dPoint2D operator *(double &k); double operator *(dPoint2D &p); double SickDistance2D(dPoint2D p); dPoint2D GetPointOnLine(dPoint2D &fP,dPoint2D &lP); double X(); double Y(); void X(double x); void Y(double y); }; dPoint2D::dPoint2D(double x,double y){ this->x = x; this->y = y; } double dPoint2D::X(){ return x; } double dPoint2D::Y(){ return y; } void dPoint2D::Y(double y){ this->y = y; } void dPoint2D::X(double x){ this->x = x; } dPoint2D dPoint2D::operator *(double &k){ dPoint2D temp(x * k,y * k); return temp; } double dPoint2D::operator *(dPoint2D &p){ return p.X() * x + p.Y() * y; } double dPoint2D::SickDistance2D(dPoint2D p){ return pow(x - p.X(),2.0) + pow(y - p.Y(),2.0); } dPoint2D dPoint2D::operator -(dPoint2D &p){ dPoint2D temp(this->x - p.X(),this->y - p.Y()); return temp; } dPoint2D dPoint2D::operator +(dPoint2D &p){ dPoint2D temp(this->x + p.X(),this->y + p.Y()); return temp; } dPoint2D dPoint2D::GetPointOnLine(dPoint2D &fP,dPoint2D &lP){ //double u = ((x - fP.X()) * (lP.X() - fP.X()) + (y - fP.Y()) * (lP.Y() - fP.Y())) / (lP.SickDistance2D(fP)); double u = (((*this) - fP) * (lP - fP))/ ((fP - lP) * (fP - lP)); return fP + (lP - fP) * u; } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){ static dPoint2D fP(20,300); static dPoint2D lP(300,20); static dPoint2D rP(0,0); // punkt rzutowany static dPoint2D zP(0,0); // punkt zrzutowany static POINT MousePos; switch(msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd,&ps); HPEN hPen = CreatePen(PS_SOLID,2,RGB(0,0,255)); SelectObject(hdc,hPen); MoveToEx(hdc,(int)fP.X(),(int)fP.Y(),NULL); LineTo(hdc,(int)lP.X(),(int)lP.Y()); MoveToEx(hdc,(int)rP.X(),(int)rP.Y(),NULL); LineTo(hdc,(int)zP.X(),(int)zP.Y()); Ellipse(hdc,(int)rP.X()-5,(int)rP.Y()-5,(int)rP.X()+5,(int)rP.Y()+5); Ellipse(hdc,(int)zP.X()-5,(int)zP.Y()-5,(int)zP.X()+5,(int)zP.Y()+5); DeleteObject(hPen); EndPaint(hWnd,&ps); } break; case WM_LBUTTONDOWN: { lP.X(MousePos.x); lP.Y(MousePos.y); zP = rP.GetPointOnLine(lP,fP); InvalidateRect(hWnd,NULL,true); } break; case WM_RBUTTONDOWN: { fP.X(MousePos.x); fP.Y(MousePos.y); zP = rP.GetPointOnLine(lP,fP); InvalidateRect(hWnd,NULL,true); } break; case WM_MOUSEMOVE: { POINT pt; MousePos.x = LOWORD(lParam); MousePos.y = HIWORD(lParam); rP.X(MousePos.x); rP.Y(MousePos.y); zP = rP.GetPointOnLine(lP,fP); InvalidateRect(hWnd,NULL,true); } break; case WM_DESTROY: { PostQuitMessage(0); } break; } return DefWindowProc(hWnd,msg,wParam,lParam); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE ,LPSTR ,int ){ WNDCLASS wnd; wnd.cbClsExtra = NULL; wnd.cbWndExtra = NULL; wnd.hbrBackground = CreateSolidBrush(RGB(0,0,0)); wnd.hCursor = LoadCursor(NULL,IDC_ARROW); wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); wnd.hInstance = hInstance; wnd.lpfnWndProc = WndProc; wnd.lpszClassName = "WndRzutowanie"; wnd.lpszMenuName = NULL; wnd.style = CS_VREDRAW|CS_HREDRAW; if(!RegisterClass(&wnd)){ MessageBox(NULL,"Coś nie tak","Informacja",MB_OK); return 0; } HWND hwnd = CreateWindow("WndRzutowanie","Rzutowanie",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL); ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,NULL,0,0)){ DispatchMessage(&msg); TranslateMessage(&msg); } return 0; }