多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一、 有关“法线角(的弧度值)”的计算方法(核心代码): ``` double FaXianjiao(double A, double x, double y) { double faXianjiao = acos((x - Apianyi_X500) / halfA); if (y < Bpianyi_Y300) { faXianjiao = -faXianjiao; } return faXianjiao; }//FaXianjiao //法线角,取原始法线反方向+Pild(+180度)后的法线角 double FaXian180jiao(double A, double x, double y) { double faXianjiao = acos((x - Apianyi_X500) / halfA); if (y < Bpianyi_Y300) { faXianjiao = -faXianjiao; } return Pild+faXianjiao; }//double FaXian180jiao ``` 二、有关“反射角计算”的核心代码: 变量 arc1先为“入射角”的弧度值: ` cou05Angle1 << "[先显示入射角:" << arc1*180/Pild << "]:"; cou05Angle1 <<"(此时)法线角:" << tmpfaXianjiao*180/Pild << "]"; //小角等于: double arc_little1 = (Pild + arc1); cou05Angle1 << "[入射角转成小角(小于90度正角):" << arc_little1*180/Pild << "]"; double jia_faXian_jiao = tmpfaXianjiao - arc_little1; cou05Angle1 << "[算出与法线夹角(小于90度正角与法线夹角):" << jia_faXian_jiao*180/Pild << "]_"; double newArc1 = (tmpfaXianjiao + 1.0*jia_faXian_jiao); cou05Angle1 << "[法线+1倍的 夹角:" << newArc1*180/Pild << "],"; // arc1 = newArc1; cou05Angle1 << "【反射角:" << arc1 << "】;" << endl; //简化步骤: // arc1 = tmpfaXianjiao + (tmpfaXianjiao - (Pild + arc1)); //再化简: arc1 = 2.0*tmpfaXianjiao - arc1 - Pild; ` 经过 (与法线角相交) 计算: arc1 变量 存储 了 “反射角”(的弧度值)! 三、 ![](https://box.kancloud.cn/0a38418fe16287524eeb888ba98a7e9f_1028x758.jpg) 四、核心(较完整)代码如下: ``` //核心(较完整)代码如下: // oneEllipse18WinPro1225a1.cpp : 定义应用程序的入口点。 #include "stdafx.h" #include "oneEllipse18WinPro1225a1.h" #include <iostream> #include <fstream> //#include <math.h> #include <iomanip> using namespace std; #define MAX_LOADSTRING 100 // 全局变量: //浮点数计PI(π): long double Pild = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196; HINSTANCE hInst; // 当前实例 WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 ofstream cou02t; ofstream cou03t; ofstream cou04t; ofstream cou05Angle1; int Y_high700 = 700; int R400 = 400; //椭圆参数 int Apianyi_X500 = 500; int Bpianyi_Y300 = 300; int Llength = 1000; double halfA = Llength / 2; double halfLittleF400 = 400; double halfB0_2 = halfA * halfA - halfLittleF400 * halfLittleF400; double halfB = sqrtf(halfB0_2); //cout << "[halfA:" << halfA << "]" << std::endl; //cout << "[halfLittleF:" << halfLittleF400 << "]" << std::endl; //cout << "[halfB:" << halfB << "]" << std::endl; //椭圆参数end // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此处放置代码。 //调用Console控制台 AllocConsole(); #pragma warning(disable:4996) _tfreopen(TEXT("CONOUT$"), TEXT("w"), stdout); _tfreopen(TEXT("CONOUT$"), TEXT("w"), stderr); _tfreopen(TEXT("CONIN$"), TEXT("r"), stdin); #pragma warning(default:4996) _tsetlocale(LC_ALL, TEXT("chs")); // cout << setprecision(99) << Pild << endl; // 初始化全局字符串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_ONEELLIPSE18WINPRO1225A1, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ONEELLIPSE18WINPRO1225A1)); MSG msg; // 主消息循环: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目标: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ONEELLIPSE18WINPRO1225A1)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_ONEELLIPSE18WINPRO1225A1); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } //画点: void setpixe01(HDC hdc, int x, int y, COLORREF rgb1) { SetPixel(hdc, x, Y_high700 - y, rgb1); } //MoveTo: void move01to(HDC hdc, int x, int y) { MoveToEx(hdc, x, Y_high700 - y, NULL); } //画线: void line01to(HDC hdc, int x, int y, COLORREF rgb1) { LineTo(hdc, x, Y_high700-y); } // //画普通角度 的线(比如法线): void line01Normal01light(HDC hdc, double arc, int R,int x0, int y0, COLORREF rgb1) { for (int i = 0; i < R; ++i) { double x_origin = i * cos(arc); double y_origin = i * sin(arc); double x = x_origin +x0; // +Apianyi_X500; double y = y_origin +y0; // Bpianyi_Y300 + y_origin; setpixe01(hdc, x, y, rgb1); } }//line01Normal01light //专画切线 //专画切线,且返回切线角 double drawTangent(HDC hdc, double arc1, int x0, int y0, COLORREF rgb1) { double x_origin=0; for (int i = 0; i < R400; ++i) { //double x_origin = -sin(arc1) ; double y_origin = cos(arc1); double x_d = x_origin * i +x0; double y_d = y_origin * i+y0; setpixe01(hdc,x_d, y_d, rgb1); } double qieXianjiao = asin(x_origin); return qieXianjiao; }//void drawTangent //专画切线-带R double drawTangentR(HDC hdc, double arc1, double r, int x0, int y0, COLORREF rgb1) { double x_origin = 0; for (int i = 0; i < r; ++i) { //double x_origin = -sin(arc1); double y_origin = cos(arc1); double x_d = x_origin * i + x0; double y_d = y_origin * i + y0; setpixe01(hdc, x_d, y_d, rgb1); } double qieXianjiao = asin(x_origin); return qieXianjiao; }//void drawTangent //返回切线角-带R double TangentR2Arc(HDC hdc, double A, int x0, int y0, COLORREF rgb1) { double qieXianjiao = asin( -1.0* (x0-500)/halfA ); return qieXianjiao; }//void drawTangent double QieXianjiao(double A, double x, double y) { //负值有问题 double qieXianjiao = acos( (x - 500) / halfA ); return qieXianjiao; } double FaXianjiao(double A, double x, double y) { double qieXianjiao = acos((x - 500) / halfA); if (y < 300) { qieXianjiao = -qieXianjiao; } return qieXianjiao; } double FaXian180jiao(double A, double x, double y) { double faXianjiao = acos((x - 500) / halfA); if (y < 300) { faXianjiao = -faXianjiao; } return Pild+faXianjiao; } //专画切线的法线-带R double drawNormLine(HDC hdc, double arc1, double r, int x0, int y0, COLORREF rgb1) { double x_origin = 0; for (int i = 0; i < r; ++i) { //double x_origin = -sin(Pild/2.0+arc1); double y_origin = cos(Pild/2.0+arc1); double x_d = x_origin * i + x0; double y_d = y_origin * i + y0; setpixe01(hdc, x_d, y_d, rgb1); } double faXianjiao = asin(x_origin); return faXianjiao; }//drawNormLine //光线在椭圆中-步进 void light01line01ellipse(HDC hdc, double X0X0, double Y0Y0, double arc01Angle1, long long lineLen, COLORREF rgb1){ // cou02t.open("f:/Cout181224cou02t_1225a1.txt"); cou03t.open("f:/Cout181224cou03t_1225a3.txt"); cou04t.open("f:/Cout181224cou04t_1225a4.txt"); cou05Angle1.open("f:/Cout181224cou05Angle1_1225a5.txt"); int r100 = 100; {//试画初始点: for (int i = 0; i < r100; ++i) {//for111 double x1 = cos(arc01Angle1)*i; double y1 = sin(arc01Angle1)*i; setpixe01(hdc, x1 + X0X0, y1 + Y0Y0, RGB(200,200, 0)); // rgb1); }//for111 }//试画初始点 //光线步进 double arc1 = arc01Angle1; //初始化: double x_origin = cos(arc1); double y_origin = sin(arc1); double x2origin = 1.0 * x_origin; double y2origin = 1.0 * y_origin; double realX01 = x2origin + X0X0; // Apianyi_X500; double realY01 = y2origin + Y0Y0;// Bpianyi_Y300; double oldX01 = X0X0;// realX01; double oldY01 = 0;// Y0Y0;// realY01; double X01 = X0X0; double Y01 = Y0Y0; //光线步进 long long ii = 0; double Yellipse_Top = 0; double Yellipse_Bot = 0; double oldYellipse_Top = Yellipse_Top; double oldYellipse_Bot = Yellipse_Bot; // for (int ii = 0; ii < lineLen; ++ii) {//for110 double ellipse1NormArc1=arc1; // {//试画椭圆的切线:qieXianjiao = asin( 1.0*(x01ellipse01_origin) / halfA) for (int i = 0; i < 361; ++i) {//for1110 double arc0101 = i * Pild / 180.0; double x01ellipse01_origin = halfA * cos(arc0101); double y01ellipse01_origin = halfB * sin(arc0101); double qieXianjiao = asin(-x01ellipse01_origin/halfA); qieXianjiao = asin( 1.0*(x01ellipse01_origin) / halfA); // line01Normal01light(hdc, qieXianjiao, 19, x01ellipse01_origin + 500, y01ellipse01_origin + 300, RGB(255, 0, 0)); line01Normal01light(hdc, QieXianjiao(halfA, x01ellipse01_origin + 500, 300), 9, x01ellipse01_origin + 500, y01ellipse01_origin + 300, RGB(255, 255, 0) ); line01Normal01light(hdc, arc0101, 9, x01ellipse01_origin + 500, y01ellipse01_origin + 300, RGB(255, 200, 0)); line01Normal01light(hdc, FaXianjiao(halfA, x01ellipse01_origin + 500, 300+ y01ellipse01_origin), 5, x01ellipse01_origin + 500, y01ellipse01_origin + 300, RGB(255, 0, 200)); //qieXianjiao, 90, x01ellipse01_origin+500, y01ellipse01_origin+300, RGB(255, 200, 0)); line01Normal01light(hdc, FaXian180jiao(halfA, x01ellipse01_origin + 500, 300 + y01ellipse01_origin), 50, x01ellipse01_origin + 500, y01ellipse01_origin + 300, RGB(255, 0, 0)); }//for1110 }//试画椭圆的切线end //光线开始步进 while (ii < lineLen) { //while110 // double x_origin = cos(arc1);//光线(入射)角,落实为坐标 double y_origin = sin(arc1); double x2origin = x_origin*ii; double y2origin = y_origin*ii; double realX01 = x2origin + X01; //Apianyi_X500; double realY01 = y2origin + Y01;// Bpianyi_Y300; //算delta_Y 坐标 //对应的椭圆y坐标 double ellipse_X1 = x2origin;//realX01 - X01; // 此处 根据 realX01坐标,计算椭圆的 上弧 和下弧 Y坐标 double arc01ellipse01 = acos( (realX01-500) / halfA); double ellipse_Y1 = (double)sin(arc01ellipse01)*halfB; double Yellipse_Top = Bpianyi_Y300 + ellipse_Y1; double Yellipse_Bot = Bpianyi_Y300 - ellipse_Y1; setpixe01(hdc, realX01, Yellipse_Top, RGB(255, 0, 255));//此处显示椭圆的 上弧 和下弧 Y坐标 setpixe01(hdc, realX01, Yellipse_Bot, RGB(255, 0, 200)); cou02t << "[realX01:" << realX01 << "]_"; cou02t << "[Yellipse_Top:" << Yellipse_Top << "]-";// << endl; cou02t << "[Yellipse_Bot:" << Yellipse_Top << "]" << endl; //判断是否 相交于椭圆: if (realX01 <= 0) { arc1 = arc1 - Pild; ii = 1; } //判断是否撞 左右 边线 else if (realX01 >= 1000) { arc1 = arc1 + Pild; ii = 2; // X01 = 999; } else { //判断是否 相交于椭圆: if ((Yellipse_Bot < realY01) && (realY01 < Yellipse_Top)) //if220 { ++ii; } else if ((realY01 <= Yellipse_Bot) || (realY01 >= Yellipse_Top)) { //if220else220 碰撞了 //而且上一个点没有碰撞,则: cou03t << "[realY01:" << realY01 << "] "; // << endl; cou03t << "[Yellipse_Bot:" << Yellipse_Bot << "]-"; cou03t << "[Yellipse_Top:" << Yellipse_Top << "] "; cou03t << "[oldY01:" << oldY01 << "]" << endl; //而且上一个点没有碰撞,则: if ((oldYellipse_Bot <= oldY01) && (oldY01 <= oldYellipse_Top)) //if330else330 这下真的是(第1次)碰撞了,因:判断出 old点在椭圆内,而real点已经在椭圆外 { // cout << "Run here12:" << endl; //算反射角: //算法线: //算切线角: double ellipse1_x1 = realX01 - 500; double ellipse1_y1 = realY01 - 300; double tmpfaXianjiao = FaXian180jiao(halfA, realX01, realY01); line01Normal01light(hdc, tmpfaXianjiao , 10, realX01, realY01, RGB(200, 0, 0) ); //法线 红色10长 //判断到底是撞 上 弧了 还是 撞下弧了呢? if (realY01 <= Yellipse_Bot)//if440 撞下弧了 { // arc1 = Pild - 1.0* ellipse1NormArc1;//3.0/4*Pild;// normArc01; cou02t << "[下弧反射角arc1New:" << arc1 << "];" << endl; } else { // arc1 = -1.0* ellipse1NormArc1;//-Pild / 2.0; // arc1 - Pild / 2.0; // -arc1;// normArc01;// -arc1; // -normArc01; cou02t << "[撞)上弧反射角arc1New:" << arc1 << "];" << endl; }//if440else440 cou05Angle1 << "[先显示入射角:" << arc1*180/Pild << "]:"; cou05Angle1 <<"(此时)法线角:" << tmpfaXianjiao*180/Pild << "]"; //小角等于: double arc_little1 = (Pild + arc1); cou05Angle1 << "[入射角转成小角(小于90度正角):" << arc_little1*180/Pild << "]"; double jia_faXian_jiao = tmpfaXianjiao - arc_little1; cou05Angle1 << "[算出与法线夹角(小于90度正角与法线夹角):" << jia_faXian_jiao*180/Pild << "]_"; double newArc1 = (tmpfaXianjiao + 1.0*jia_faXian_jiao); cou05Angle1 << "[法线+1倍的 夹角:" << newArc1*180/Pild << "],"; arc1 = newArc1; cou05Angle1 << "【反射角:" << arc1 << "】;" << endl; ii = 10;// 2;// 1; //判断到底是撞 上 弧了 还是 撞下弧了end double tmpX01 = X01; double tmpY01 = Y01; //交换 X01 和 old X01; X01 = oldX01; Y01 = oldY01;// realY01; oldX01 = tmpX01; oldY01 = tmpX01; // cout << "[normArc01:" << normArc01 << "]" << endl; } else//if330else330 { cou04t << "[Run here23!" << endl; cou04t << "B2[realY01:" << realY01 << "] "; // << endl; cou04t << "[Yellipse_Bot:" << Yellipse_Bot << "]-"; cou04t << "[Yellipse_Top:" << Yellipse_Top << "] "; cou04t << "[ellipse1NormArc1:" << ellipse1NormArc1 << "]" << endl; ++ii; // system("pause"); } }//else220if220 else { ++ii; cout << "[Run wrong12!]" << endl; system("pause"); } //if220else220 } oldX01 = realX01; oldY01 = realY01; //相当于把此点(old点)的情况 入栈 oldYellipse_Bot = Yellipse_Bot; oldYellipse_Top = Yellipse_Top; //判断是否 相交于椭圆end setpixe01(hdc, realX01, realY01, RGB(100, 0, 0)); // }//while110 // }//for110 //光线步进end }//light01line01ellipse void wm01pain01ellipse( HDC hdc) { //画椭圆 {//椭圆参数 // int ApianyiX0 = 500; // int BpianyiY0 = 400; // int Llength = 1000; // double halfA = Llength / 2; // double halfLittleF400 = 400; // double halfB0_2 = halfA * halfA - halfLittleF400 * halfLittleF400; // double halfB = sqrtf(halfB0_2); std::cout << "[halfA:" << halfA << "]" << std::endl; std::cout << "[halfLittleF:" << halfLittleF400 << "]" << std::endl; std::cout << "[halfB:" << halfB << "]" << std::endl; }//椭圆参数end // {//画个圆 double R400 = 400; for (int i = 0; i < 35; ++i) { double arc1 = i * Pild / 180 *10; double x_origin = cos(arc1)* R400; double y_origin = sin(arc1)*R400; double x = x_origin + 400; double y = y_origin + 300; setpixe01(hdc, x, y, RGB(0,200,0)); } }//画个圆 {//画椭圆 int Apianyi_X500 = 500; int Bpianyi_Y300 = 300; for (int i = 0; i < 3601; ++i) { double arc1 = i * Pild / 180; double x_origin = halfA * cos(arc1); double y_origin = halfB * sin(arc1); double x = x_origin + Apianyi_X500;// 400; double y = y_origin + Bpianyi_Y300;// 300; setpixe01(hdc, x, y, RGB(0, 255, 0)); {//算切线角,画切线: double tangentArc1 = arc1; drawTangentR(hdc,tangentArc1,19, x, y, RGB(0,0,200) );//切线Blue蓝色,切线长度190 //给一个坐标(椭圆弧上的坐标),算出 法线 或者切线 {//算法线,画法线: double normArc1 = Pild / 2.0 + tangentArc1; //drawTangent(hdc, normArc1, x, y, RGB(0, 200,0)); drawNormLine(hdc, tangentArc1, 9,x, y, RGB(0, 200, 0) );//法线绿色,长度90 } }//算切线角,画切线end } //画固定点-及固定点间的连线 move01to(hdc, 100, 300); line01to(hdc, 500, 300, RGB(0,200,0) ); line01to(hdc, 900, 300, RGB(0, 200, 0)); }//画椭圆end {//画光线-及步进 int x0 = 600; int y0 = 100; double arc1 = -3.0 / 16.0*Pild; //120 度= 120/180*Pild; arc1 = -120.0/180.0*Pild; long long lineLen = 9299;// 9999;// 999;// 99;// light01line01ellipse(hdc, x0, y0, arc1, lineLen, RGB(200,0,0)); }//画光线-及步进end }//wm01pain01ellipse// // 函数: InitInstance(HINSTANCE, int) // // 目标: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 // HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目标: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: {//case110 PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此处添加使用 hdc 的任何绘图代码... // wm01pain01ellipse(hdc); // EndPaint(hWnd, &ps); }//case110 break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “关于”框的消息处理程序。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } ```