Answer the question
In order to leave comments, you need to log in
Graphics from scratch C++ what's the problem with the code?
The point is this. We study 3D graphics in C ++ without using ready-made libraries with graphics.
I have a kind of correct mathematical model of the object (hammer):
Only when compiling the code, a set of lines in the window...
Perhaps I did not describe the problem correctly (I hope that this is not so).
#include <iostream>
#include <conio.h>
#include <winbgim.h>
#include <math.h>
#define ESC 27
#define LEFT 75
#define RIGHT 77
#define TOP 72
#define BOT 80
#define ROTX 100
#define ROTY 119
#define ROTZ 97
#define RSZP 120
#define RSZM 122
struct Dot3d
{
int x, y, z;
};
Dot3d Cube[11];
int Rebro=20;
int Rebra[14][2]={{1,2},{2,3},{3,4},{5,6},{6,7}}; //////////////Первые ребра(линини) вообще не показывает, поетому дальше не прописовал
int AngleX=0;
int AngleY=0;
int AngleZ=0;
void PaintCube(Dot3d *);
void InitCube(int, int, int, int);
void OffsetCubeHorizontal(int);
void OffsetCubeVertical(int);
void OffsetCubeResize(int);
void rotationX(int);
void rotationZ(int);
void rotationY(int);
void ResizePlus(int);
void ResizeMinus(int);
int main(int argc, char** argv) {
int Krok=5;
int Mx, My;
initwindow(1200, 1000);
Mx = getmaxx()/2;;
My = getmaxy()/2;;
InitCube(200,200,0, Rebro);
PaintCube(Cube);
int v;
while (getch()!= ESC)
{
// if (kbhit())
{
v=getch();
if (v== RIGHT)
{
OffsetCubeHorizontal(Krok);
};
if (v== LEFT)
{
OffsetCubeHorizontal(-Krok);
};
if (v== BOT)
{
OffsetCubeVertical(Krok);
};
if (v== TOP)
{
OffsetCubeVertical(-Krok);
};
if (v== ROTX)
{
rotationX(15) ;
}
if (v== ROTY)
{
rotationY(15) ;
}
if (v== ROTZ)
{
rotationZ(15) ;
}
if (v== RSZM)
{
ResizeMinus(5) ;
}
if (v== RSZP)
{
ResizePlus(5) ;
}
std::cout<<v;
}
cleardevice();
PaintCube(Cube);
// delay(50);
}
getch();
return 0;
}
void InitCube(int x1, int y1, int z1, int m)
{
Cube[0].x=x1; Cube[0].y=y1; Cube[0].z=z1;
Cube[1].x=Cube[0].x; Cube[1].y=Cube[0].y+20*m; Cube[1].z=Cube[0].z;
Cube[2].x=Cube[0].x-5*m; Cube[2].y=Cube[0].y+20*m; Cube[2].z=Cube[0].z+7*m;
Cube[3].x=Cube[0].x-5*m; Cube[3].y=Cube[0].y+20*m; Cube[3].z=Cube[0].z+7*m;
Cube[4].x=Cube[0].x+5*m; Cube[4].y=Cube[0].y+20*m; Cube[4].z=Cube[0].z-7*m;
Cube[5].x=Cube[0].x+5*m; Cube[5].y=Cube[0].y+20*m; Cube[5].z=Cube[0].z+7*m;
Cube[6].x=Cube[0].x+5*m; Cube[6].y=Cube[0].y+12*m; Cube[6].z=Cube[0].z-7*m;
Cube[7].x=Cube[0].x+5*m; Cube[7].y=Cube[0].y+12*m; Cube[7].z=Cube[0].z-7*m;
Cube[8].x=Cube[0].x-5*m; Cube[8].y=Cube[0].y+12*m; Cube[8].z=Cube[0].z+7*m;
Cube[9].x=Cube[0].x-5*m; Cube[9].y=Cube[0].y+12*m; Cube[9].z=Cube[0].z-7*m;
Cube[10].x=Cube[0].x; Cube[10].y=Cube[0].y+12*m; Cube[10].z=Cube[0].z;
Cube[11].x=Cube[0].x+5*m; Cube[11].y=Cube[0].y; Cube[11].z=Cube[0].z;
for(int i=1; i<=12; i++)
{
Cube[i].x=Cube[0].x+(Cube[i].x-Cube[0].x)*cos(AngleY*3.14/180)-(Cube[i].z-Cube[0].z)*sin(AngleY*3.14/180);
Cube[i].z=Cube[0].z+(Cube[i].x-Cube[0].x)*sin(AngleY*3.14/180)+(Cube[i].z-Cube[0].z)*cos(AngleY*3.14/180);
Cube[i].y=Cube[0].y+(Cube[i].y-Cube[0].y)*cos(AngleX*3.14/180)-(Cube[i].y-Cube[0].y)*sin(AngleX*3.14/180);
Cube[i].z=Cube[0].z+(Cube[i].z-Cube[0].z)*sin(AngleX*3.14/180)+(Cube[i].z-Cube[0].z)*cos(AngleX*3.14/180);
Cube[i].x=Cube[0].x+(Cube[i].x-Cube[0].x)*cos(AngleZ*3.14/180)-(Cube[i].y-Cube[0].y)*sin(AngleZ*3.14/180);
Cube[i].y=Cube[0].y+(Cube[i].x-Cube[0].x)*sin(AngleZ*3.14/180)+(Cube[i].y-Cube[0].y)*cos(AngleZ*3.14/180);
}
for(int i=1; i<=12; i++)
{
Cube[i].y=Cube[0].y+(Cube[i].y-Cube[0].y)*cos(AngleX*3.14/180)-(Cube[i].y-Cube[0].y)*sin(AngleX*3.14/180);
Cube[i].z=Cube[0].z+(Cube[i].z-Cube[0].z)*sin(AngleX*3.14/180)+(Cube[i].z-Cube[0].z)*cos(AngleX*3.14/180);
}
for(int i=1; i<=12; i++)
{
Cube[i].x=Cube[0].x+(Cube[i].x-Cube[0].x)*cos(AngleZ*3.14/180)-(Cube[i].y-Cube[0].y)*sin(AngleZ*3.14/180);
Cube[i].y=Cube[0].y+(Cube[i].x-Cube[0].x)*sin(AngleZ*3.14/180)+(Cube[i].y-Cube[0].y)*cos(AngleZ*3.14/180);
}
//////////////////////////////////////////
}
void PaintCube(Dot3d *c)
{
int yt1, yt2, xt1, xt2;
double koefZX=sin(AngleX*3.14/180);
double koefZY=cos(AngleY*3.14/180);
koefZX=0.3;
koefZY=0.2;
for(int i=1; i<=12; i++)
{
xt1=c[Rebra[i][0]].x-c[Rebra[i][0]].z*koefZX;
yt1=c[Rebra[i][0]].y-c[Rebra[i][0]].z*koefZY;
xt2=c[Rebra[i][1]].x-c[Rebra[i][1]].z*koefZX;
yt2=c[Rebra[i][1]].y-c[Rebra[i][1]].z*koefZY;
setcolor(i+2);
line(xt1, yt1, xt2, yt2);
// std::cout<<Rebra[i][1]<<" "<<Rebra[i][2]<<std::endl;
}
}
void OffsetCubeHorizontal(int ofs)
{
for(int i=1; i<=12; i++)
{
Cube[i].x=Cube[i].x+ofs;
}
}
void OffsetCubeVertical(int ofs)
{
for(int i=1; i<=12; i++)
{
Cube[i].y=Cube[i].y+ofs;
}
}
void OffsetCubeResize(int r)
{
Rebro=Rebro+r;
}
void rotationX(int a)
{
AngleX=AngleX+a;
InitCube(Cube[0].x, Cube[0].y, Cube[0].y, Rebro);
}
void rotationY(int a)
{
AngleY=AngleY+a;
InitCube(Cube[0].x, Cube[0].y, Cube[0].y, Rebro);
}
void rotationZ(int a)
{
AngleZ=AngleZ+a;
InitCube(Cube[0].x, Cube[0].y, Cube[0].y, Rebro);
}
void ResizePlus(int r)
{
Rebro=Rebro+r;
InitCube(Cube[0].x, Cube[0].y, Cube[0].y, Rebro);
}
void ResizeMinus(int r)
{
Rebro=abs(Rebro-r);
InitCube(Cube[0].x, Cube[0].y, Cube[0].y, Rebro);
}
Answer the question
In order to leave comments, you need to log in
You have at least problems with dimensionality
Dot3d Cube[11];
, which means you can only access by numbers 0-10
and you do loops from 1-12
The same with
int Rebra[14][2]={{1,2},{2,3},{3,4},{5,6},{6,7}};
Starting from the fact that this is clearly not an int type
and also in the code you access from 1-12, but range 0-13.
It's obvious that it's all wrong.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question