LightGunDiyer 光枪爱好者
github方案移植 步骤二 方案的代码部分Transformation - 可打印的版本

+- LightGunDiyer 光枪爱好者 (http://www.wukongxuetang.com/bbs)
+-- 版块: 光枪原理与光枪DIY (http://www.wukongxuetang.com/bbs/forumdisplay.php?fid=1)
+--- 版块: 光枪-创意设计 (http://www.wukongxuetang.com/bbs/forumdisplay.php?fid=6)
+--- 主题: github方案移植 步骤二 方案的代码部分Transformation (/showthread.php?tid=31)



github方案移植 步骤二 方案的代码部分Transformation - wukong - 04-29-2024

此部分为作者自己编写的光点转换函数

class Transformation{

  private:
  int* x;
  int* y;

  int pointX;
  int pointY;

  int screenWidth;
  int screenHeight;

  double x1(double varX, double varY);
  double x2(double varX, double varY);
  double x3(double varX, double varY);
  double x4(double varX, double varY);
 
  double y1(double varX, double varY);
  double y2(double varX, double varY);
  double y3(double varX, double varY);
  double y4(double varX, double varY);

  public:
  Transformation(int* pX, int* pY, int pScreenWidth, int pScreenHeight, int pPointX, int pPointY);
  int u();
  int v();
 
};


#以上为函数的头文件部分,为一个结构类声明
#以下为函数的cpp文件,与设定的屏幕分辨率进行对应

#include "Transformation.h"

Transformation::Transformation(int* pX, int* pY, int pScreenWidth, int pScreenHeight, int pPointX, int pPointY) {
  x = pX;
  y = pY;
  screenWidth = pScreenWidth;
  screenHeight = pScreenHeight;
  pointX = pPointX;
  pointY = pPointY;
}

//声明x1函数,获取坐标与x0之间的x值 只使用第一个参数,问什么要声明2个参数呢
double Transformation::x1(double varX, double varY) {
  return varX - x[0];
}

//声明x2函数,内部调用x1函数,不明白问什么要多声明一个
double Transformation::x2(double varX, double varY) {
  return x1(varX, varY);
}

double Transformation::x3(double varX, double varY) {
  return x2(varX, varY) - y2(varX, varY) * x2(x[2], y[2]) / y2(x[2], y[2]);
}

double Transformation::x4(double varX, double varY) {
  return (((x3(x[1], y[1]) / x3(x[3], y[3])) - 1) * (y3(varX, varY) / y3(x[3], y[3])) + 1) * x3(varX, varY);
}

//换算到屏幕位置x
int Transformation::u() {
  return (int) (x4(pointX, pointY) * screenWidth / x4(x[3], y[3]));
}

//声明y1函数 获取坐标与y0之间的y值
double Transformation::y1(double varX, double varY) {
  return varY - y[0];
}

//获取坐标到顶边的y值
double Transformation::y2(double varX, double varY) {
  return y1(varX, varY) - x1(varX, varY) * y1(x[1], y[1]) / x1(x[1], y[1]);
}

//声明函数y3,函数调用y2
double Transformation::y3(double varX, double varY) {
  return y2(varX, varY);
}

double Transformation::y4(double varX, double varY) {
  return (((y3(x[2], y[2]) / y3(x[3], y[3])) - 1) * (x3(varX, varY) / x3(x[3], y[3])) + 1) * y3(varX, varY);
}

//换算到屏幕位置y
int Transformation::v() {
  return (int) (y4(pointX, pointY) * screenHeight / y4(x[3], y[3]));
}