Spaceship War.cpp
C++#include<conio.h>
#include<windows.h>
#include<bits/stdc++.h>
#include "geometry.h"
using namespace std;
struct COORDINATE{
Vector3 Coordinate[3];
COORDINATE(){
Coordinate[0]=Point3(1,0,0),Coordinate[1]=Point3(0,1,0),Coordinate[2]=Point3(0,0,1);
}void TurnUp(const LF degree){
Coordinate[1]=RotatePointAroundVector(Coordinate[1],Coordinate[0],degree);
Coordinate[2]=RotatePointAroundVector(Coordinate[2],Coordinate[0],degree);
}void TurnDown(const LF degree){
Coordinate[1]=RotatePointAroundVector(Coordinate[1],Coordinate[0],-degree);
Coordinate[2]=RotatePointAroundVector(Coordinate[2],Coordinate[0],-degree);
}void TurnLeft(const LF degree){
Coordinate[0]=RotatePointAroundVector(Coordinate[0],Coordinate[2],degree);
Coordinate[1]=RotatePointAroundVector(Coordinate[1],Coordinate[2],degree);
}void TurnRight(const LF degree){
Coordinate[0]=RotatePointAroundVector(Coordinate[0],Coordinate[2],-degree);
Coordinate[1]=RotatePointAroundVector(Coordinate[1],Coordinate[2],-degree);
}
};
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
CONSOLE_FONT_INFOEX font;
#define CursorTo(y,x) coord.Y=y,coord.X=x;SetConsoleCursorPosition(hout,coord);
#define Font_siz_to(y,x) font.dwFontSize.Y=y,font.dwFontSize.X=x;SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE),FALSE,&font);
struct CAMERA{
Point3 Location;
COORDINATE Coordinate;
LF Angle;
CAMERA(){}
CAMERA(const Point3 Location,const LF Angle):Location(Location),Angle(Angle){}
LF GetCenterX(const int ScreenSize,const Point3 point){
const LF NewX=TetrahedronVolume6(Location,Location+Coordinate.Coordinate[1],Location+Coordinate.Coordinate[2],point);
const LF NewY=TetrahedronVolume6(Location,Location+Coordinate.Coordinate[2],Location+Coordinate.Coordinate[0],point);
return round((ScreenSize>>1)+NewX/tan(Radian(Angle/2))/NewY*(ScreenSize>>1));
}LF GetCenterY(const int ScreenSize,const Point3 point){
const LF NewY=TetrahedronVolume6(Location,Location+Coordinate.Coordinate[2],Location+Coordinate.Coordinate[0],point);
const LF NewZ=TetrahedronVolume6(Location,Location+Coordinate.Coordinate[0],Location+Coordinate.Coordinate[1],point);
return round((ScreenSize>>1)-NewZ/tan(Radian(Angle/2))/NewY*(ScreenSize>>1));
}void OutputSphere(const int ScreenSize,Sphere sphere){
const LF NewR=max(tan(asin(sphere.R/Distance(Location,sphere.Center)))/tan(Radian(Angle/2))*ScreenSize/2,(LF)1);
const int CenterX=GetCenterX(ScreenSize,sphere.Center);
const int CenterY=GetCenterY(ScreenSize,sphere.Center);
for(int i=max((LF)0,CenterY-NewR)+1;i<=min((LF)(0+ScreenSize),CenterY+NewR);i++){
for(int j=max((LF)0,CenterX-sqrt(NewR*NewR-(i-CenterY)*(i-CenterY)));j<=min((LF)ScreenSize,CenterX+sqrt(NewR*NewR-(i-CenterY)*(i-CenterY)));j++){
CursorTo(i,j);
putchar('#');
}
}
}static bool DecideOutputOrder(const Sphere A,const Sphere B){
return VectorLen(A.Center)-A.R>VectorLen(B.Center)-B.R;
}void OutputSphere(const int ScreenSize,const vector<Sphere>sphere){
vector<Sphere>tmp;
for(Sphere i:sphere){
tmp.push_back(Sphere(i.Center-Location,i.R));
}sort(tmp.begin(),tmp.end(),DecideOutputOrder);
for(Sphere i:tmp){
OutputSphere(ScreenSize,i);
}
}
};
vector<Sphere>Sun;
struct SPACESHIP{
Point3 Location;
Vector3 Direction;
COORDINATE Coordinate;
CAMERA Camera;
SPACESHIP(){
Direction=Point3(0,0,0);
}SPACESHIP(const Point3 Location,const LF Angle):Location(Location),Camera(Location,Angle){}
void OutputCatch(int ScreenSize){
Camera.OutputSphere(ScreenSize,Sun);
CursorTo(0,0);
printf("当前坐标:(%.3LF,%.3LF,%.3lf)\n",Location.x,Location.y,Location.z);
printf("当前位置:(%.3LF,%.3LF,%.3lf)\n",Coordinate.Coordinate[1].x,Coordinate.Coordinate[1].y,Coordinate.Coordinate[1].z);
printf("当前速度:%.3lf speed of light",VectorLen(Direction));
}void Move(){
Location=Location+Direction;
Camera.Location=Camera.Location+Direction;
}void Control(const LF RotateAngle){
if(kbhit()){
char c=getch();
if(c=='+'){
if(dcmp(VectorLen(Direction+Coordinate.Coordinate[1]/1000),1)<=0){
Direction=Direction+Coordinate.Coordinate[1]/1000;
}
}else if(c=='-'){
if(dcmp(VectorLen(Direction-Coordinate.Coordinate[1]/1000),0)>=0){
Direction=Direction-Coordinate.Coordinate[1]/1000;
}
}else if(c=='*'){
Direction=Direction*2.0;
}else if(c=='/'){
Direction=Direction/2.0;
}else if(c=='0'){
Direction.x=Direction.y=Direction.z=0;
}else if(c=='W'){
Coordinate.TurnUp(RotateAngle);
Camera.Coordinate.TurnUp(RotateAngle);
Direction=Coordinate.Coordinate[1]*VectorLen(Direction);
}else if(c=='A'){
Coordinate.TurnLeft(RotateAngle);
Camera.Coordinate.TurnLeft(RotateAngle);
Direction=Coordinate.Coordinate[1]*VectorLen(Direction);
}else if(c=='S'){
Coordinate.TurnDown(RotateAngle);
Camera.Coordinate.TurnDown(RotateAngle);
Direction=Coordinate.Coordinate[1]*VectorLen(Direction);
}else if(c=='D'){
Coordinate.TurnRight(RotateAngle);
Camera.Coordinate.TurnRight(RotateAngle);
Direction=Coordinate.Coordinate[1]*VectorLen(Direction);
}else if(c=='w'){
Camera.Coordinate.TurnUp(RotateAngle);
}else if(c=='a'){
Camera.Coordinate.TurnLeft(RotateAngle);
}else if(c=='s'){
Camera.Coordinate.TurnDown(RotateAngle);
}else if(c=='d'){
Camera.Coordinate.TurnRight(RotateAngle);
}else if(c==')'){
for(int i=0;i<3;i++){
Camera.Coordinate.Coordinate[i]=Coordinate.Coordinate[i];
}
}
}
}void Command(const int ScreenSize=50,const LF RotateAngle=2.0){
OutputCatch(ScreenSize);
Move();
Control(RotateAngle);
}
}Spaceship;
long long read(){
long long ret=0;
char c=getchar();
while(c>='0'&&c<='9'){
if(ceil((LONG_LONG_MAX-(c-'0')*1.0)/10)<ret){
ret=-1;
break;
}ret=ret*10+c-'0';
c=getchar();
}while(c!='\n'){
c=getchar();
}return ret;
}void Init(){
printf("请输入您需要的恒星数量:");
while(true){
long long Input=read();
if(Input>=0&&Input<=1e5){
printf("%d颗恒星已生成\n");
system("pause");
for(int i=1;i<=Input;i++){
Point3 tmp;
tmp.rand();
Sun.push_back(Sphere);
}break;
}printf("输入错误,请重新输入:");
}
}signed main(){
Init();
while(true){
system("cls");
Spaceship.Command();
}
return 0;
}
geometry.h
C++#include<bits/stdc++.h>
using namespace std;
#define LF long double
const LF eps=1e-8,pi=acos(-1);
int sgn(const LF num){
if(fabs(num)<eps){
return 0;
}return num>0?1:-1;
}int dcmp(const LF num_1,const LF num_2){
if(sgn(num_1-num_2)==0){
return 0;
}return num_1-num_2>0?1:-1;
}LF RandFrom0to1(){
return rand()*1.0/RAND_MAX;
}LF Rand(const LF MinNum=INT_MIN,const LF MaxNum=INT_MAX){
return MinNum+RandFrom0to1()*(MaxNum-MinNum);
}
struct Point2{
LF x,y;
Point2(){}
Point2(const LF x,const LF y):x(x),y(y){}
const Point2 operator+(const Point2 &B)const{
return Point2(x+B.x,y+B.y);
}const Point2 operator-(const Point2 &B)const{
return Point2(x-B.x,y-B.y);
}const Point2 operator*(const double &B)const{
return Point2(x*B,y*B);
}const Point2 operator/(const double &B)const{
return Point2(x/B,y/B);
}bool operator==(const Point2 &B)const{
return dcmp(x,B.x)==0&&dcmp(y,B.y)==0;
}void rand(const LF MinNum=-INT_MAX,const LF MaxNum=INT_MAX){
x=Rand(MinNum,MaxNum);
y=Rand(MinNum,MaxNum);
}
};
#define Vector2 Point2
struct Line2{
Point2 p_1,p_2;
Line2(const Point2 p_1,const Point2 p_2):p_1(p_1),p_2(p_2){}
};
#define Segment2 Line2
LF Dot(const Vector2 A,const Vector2 B){
return A.x*B.x+A.y*B.y;
}LF Cross(const Vector2 A,const Vector2 B){
return A.x*B.y-A.y*B.x;
}LF Distance(const Point2 A,const Point2 B){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}LF VectorLen(const Vector2 A){
return sqrt(Dot(A,A));
}LF VectorLen2(const Vector2 A){
return Dot(A,A);
}LF IncludedAngle(const Vector2 A,const Vector2 B){
return acos(Dot(A,B)/VectorLen(A)/VectorLen(B));
}LF TriangleArea(const Point2 A,const Point2 B,const Point2 C){
return Cross(B-A,C-A)/2;
}bool PointOnSegment(const Point2 point,const Segment2 segment){
return sgn(Cross(point-segment.p_1,point-segment.p_2))==0&&sgn(Dot(point-segment.p_1,point-segment.p_2))<=0;
}
struct Point3{
LF x,y,z;
Point3(){}
Point3(LF x,LF y,LF z):x(x),y(y),z(z){}
const Point3 operator+(const Point3 &B)const{
return Point3(x+B.x,y+B.y,z+B.z);
}const Point3 operator-(const Point3 &B)const{
return Point3(x-B.x,y-B.y,z-B.z);
}const Point3 operator*(const LF &B)const{
return Point3(x*B,y*B,z*B);
}const Point3 operator/(const LF &B)const{
return Point3(x/B,y/B,z/B);
}bool operator==(const Point3 &B){
return dcmp(x,B.x)==0&&dcmp(y,B.y)==0&&dcmp(z,B.z)==0;
}void rand(const LF MinNum=-INT_MAX,const LF MaxNum=INT_MAX){
x=Rand(MinNum,MaxNum);
y=Rand(MinNum,MaxNum);
z=Rand(MinNum,MaxNum);
}
};
#define Vector3 Point3
struct Line3{
Point3 p_1,p_2;
Line3(){}
Line3(const Point3 p_1,const Point3 p_2):p_1(p_1),p_2(p_2){}
};
#define Segment3 Line3
struct Plane{
Point3 p_1,p_2,p_3;
Plane(){}
Plane(const Point3 p_1,const Point3 p_2,const Point3 p_3):p_1(p_1),p_2(p_2),p_3(p_3){}
};
LF Dot(const Vector3 A,const Vector3 B){
return A.x*B.x+A.y*B.y+A.z*B.z;
}Vector3 Cross(const Vector3 A,const Vector3 B){
return Vector3(A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x);
}LF Distance(const Point3 A,const Point3 B){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z));
}LF VectorLen(const Vector3 A){
return sqrt(Dot(A,A));
}LF VectorLen2(const Vector3 A){
return Dot(A,A);
}LF IncludedAngle(const Vector3 A,const Vector3 B){
return acos(Dot(A,B)/VectorLen(A)/VectorLen(B));
}LF TriangleArea2(const Point3 TrianglePoint_1,const Point3 TrianglePoint_2,const Point3 TrianglePoint_3){
return VectorLen(Cross(TrianglePoint_2-TrianglePoint_1,TrianglePoint_3-TrianglePoint_1));
}bool Point_in_triangle(const Point3 TrianglePoint_1,const Point3 TrianglePoint_2,const Point3 TrianglePoint_3,const Point3 point){
return dcmp(TriangleArea2(point,TrianglePoint_1,TrianglePoint_2)+TriangleArea2(point,TrianglePoint_2,TrianglePoint_3)+TriangleArea2(point,TrianglePoint_3,TrianglePoint_1),TriangleArea2(TrianglePoint_1,TrianglePoint_2,TrianglePoint_3))==0;
}bool PointOnLine(const Point3 point,const Line3 line){
return sgn(VectorLen(Cross(line.p_1-point,line.p_2-point)))==0&&sgn(Dot(line.p_1-point,line.p_2-point))==0;
}Vector3 NormalVector(const Plane plane){
return Cross(plane.p_2-plane.p_1,plane.p_3-plane.p_1);
}LF PointOnPlane(const Plane plane,const Point3 point){
return sgn(Dot(NormalVector(plane),point-plane.p_1))==0;
}bool PlanesParallel(const Plane A,const Plane B){
return VectorLen(Cross(NormalVector(A),NormalVector(B)))<eps;
}LF TetrahedronVolume6(const Point3 A,const Point3 B,const Point3 C,const Point3 D){
return Dot(Cross(B-A,C-A),D-A);
}LF Radian(const LF degree){
return degree*pi/180;
}LF Degree(const LF radian){
return radian*180/pi;
}Point3 RotatePointAroundVector(const Point3 point,const Vector3 vector,const LF degree){
const LF s=sin(Radian(degree)),c=cos(Radian(degree));
return Point3(
(vector.x*vector.x*(1-c)+c)*point.x+(vector.x*vector.y*(1-c)-vector.z*s)*point.y+(vector.x*vector.z*(1-c)+vector.y*s)*point.z,
(vector.x*vector.y*(1-c)+vector.z*s)*point.x+(vector.y*vector.y*(1-c)+c)*point.y+(vector.y*vector.z*(1-c)-vector.x*s)*point.z,
(vector.x*vector.z*(1-c)-vector.y*s)*point.x+(vector.y*vector.z*(1-c)+vector.x*s)*point.y+(vector.z*vector.z*(1-c)+c)*point.z);
}Point3 RotatePointAroundVector(const Point3 point,const Point3 vectorLocation,const Vector3 vector,const LF degree){
return RotatePointAroundVector(point-vectorLocation,vector,degree)+vectorLocation;
}
struct Sphere{
Point3 Center;
LF R;
Sphere(){}
Sphere(const Point3 Center,const double R):Center(Center),R(R){}
};