Working 2d square
This commit is contained in:
parent
056efe3c08
commit
d2e0f1da56
@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
#define PI 3.1415
|
#define PI 3.1415
|
||||||
|
|
||||||
|
const int VIEW_SIZE = 4.0f;
|
||||||
|
|
||||||
std::vector<float> matrixMul(std::vector<float> p, std::vector<float> m)
|
std::vector<float> matrixMul(std::vector<float> p, std::vector<float> m)
|
||||||
{
|
{
|
||||||
std::vector<float> res(3);
|
std::vector<float> res(3);
|
||||||
@ -32,6 +34,16 @@ std::vector<float> matrixMul(std::vector<float> p, std::vector<float> m)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<float> MyWindow::GetXY(Point p, std::vector<float> m)
|
||||||
|
{
|
||||||
|
std::vector<float> res(3);
|
||||||
|
res[0] = ((p.x + VIEW_SIZE / 2.0f) / VIEW_SIZE * szer );
|
||||||
|
res[1] = ((p.y + VIEW_SIZE / 2.0f) / VIEW_SIZE * wys);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<float> matrixMul3x3(std::vector<float> m1, std::vector<float> m2)
|
std::vector<float> matrixMul3x3(std::vector<float> m1, std::vector<float> m2)
|
||||||
{
|
{
|
||||||
std::vector<float> res(9, 0);
|
std::vector<float> res(9, 0);
|
||||||
@ -48,6 +60,21 @@ std::vector<float> matrixMul3x3(std::vector<float> m1, std::vector<float> m2)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<float> matrixMul4x4(std::vector<float> m1, std::vector<float> m2)
|
||||||
|
{
|
||||||
|
std::vector<float> res(16, 0);
|
||||||
|
for(int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
for(int k = 0; k < 4; k++)
|
||||||
|
{
|
||||||
|
res[i * 4 + j] += m1[i * 4 + k] * m2[k * 4 + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
// Definicja konstruktora, wywolujemy najpierw
|
// Definicja konstruktora, wywolujemy najpierw
|
||||||
// konstruktor klasy nadrzednej, nastepnie tworzymy
|
// konstruktor klasy nadrzednej, nastepnie tworzymy
|
||||||
// obiekt klasy Ui_MyWindow reprezentujacy GUI
|
// obiekt klasy Ui_MyWindow reprezentujacy GUI
|
||||||
@ -81,6 +108,17 @@ MyWindow::MyWindow(QWidget *parent) :
|
|||||||
translation_vec.push_back(0);
|
translation_vec.push_back(0);
|
||||||
translation_vec.push_back(0);
|
translation_vec.push_back(0);
|
||||||
|
|
||||||
|
// points[0] = Point(-1.0f, -1.0f, 0.0f);
|
||||||
|
// points[1] = Point(1.0f, -1.0f, 0.0f);
|
||||||
|
// points[2] = Point(1.0f, 1.0f, 0.0f);
|
||||||
|
// points[3] = Point(-1.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
|
// points[4] = Point(-1.0f, -1.0f, 0.0f);
|
||||||
|
// points[5] = Point(1.0f, -1.0f, 0.0f);
|
||||||
|
// points[6] = Point(1.0f, 1.0f, 0.0f);
|
||||||
|
// points[7] = Point(-1.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
|
|
||||||
scale_vec.push_back(1.0f);
|
scale_vec.push_back(1.0f);
|
||||||
scale_vec.push_back(1.0f);
|
scale_vec.push_back(1.0f);
|
||||||
|
|
||||||
@ -142,59 +180,73 @@ void MyWindow::UpdateImage(){
|
|||||||
float cosa = cos(rotation_angle * 0.01745329252f);
|
float cosa = cos(rotation_angle * 0.01745329252f);
|
||||||
|
|
||||||
std::vector<float> translate_mat = {
|
std::vector<float> translate_mat = {
|
||||||
1, 0, translation_vec[0],
|
1, 0, 0, translation_vec[0],
|
||||||
0, 1, translation_vec[1],
|
0, 1, 0, translation_vec[1],
|
||||||
0, 0, 1,
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<float> scale_mat = {
|
std::vector<float> scale_mat = {
|
||||||
scale_vec[0], 0, 0,
|
scale_vec[0], 0, 0, 0,
|
||||||
0, scale_vec[1], 0,
|
0, scale_vec[1], 0, 0,
|
||||||
0, 0, 1,
|
0, 0, scale_vec[1], 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<float> rotation_mat = {
|
std::vector<float> rotation_mat_x = {
|
||||||
cosa, -sina, 0,
|
1, 0, 0, 0,
|
||||||
sina, cosa, 0,
|
0, cosa, -sina, 0,
|
||||||
0, 0, 1,
|
0, sina, cosa, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<float> shx_mat = {
|
std::vector<float> rotation_mat_y = {
|
||||||
1, sh_vec[0], 0,
|
cosa, 0, sina, 0,
|
||||||
0, 1, 0,
|
0, 1, 0, 0,
|
||||||
0, 0, 1,
|
-sina, 0, cosa, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<float> rotation_mat_z = {
|
||||||
std::vector<float> shy_mat = {
|
cosa, -sina, 0, 0,
|
||||||
1, 0, 0,
|
sina, cosa, 0, 0,
|
||||||
sh_vec[1], 1, 0,
|
0, 0, 1, 0,
|
||||||
0, 0, 1,
|
0, 0, 0, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<float> tansform_mat = matrixMul3x3(matrixMul3x3(matrixMul3x3(matrixMul3x3(
|
std::vector<float> sh_mat = {
|
||||||
scale_mat,
|
1, 0, sh_vec[0], 0,
|
||||||
rotation_mat),
|
0, 1, sh_vec[1], 0,
|
||||||
shx_mat),
|
0, 0, 1, 0,
|
||||||
shy_mat),
|
0, 0, 0, 1,
|
||||||
translate_mat);
|
};
|
||||||
|
|
||||||
for(int x = 0; x < szer; x++)
|
std::vector<float> tansform_mat =
|
||||||
for(int y = 0; y < wys; y++){
|
matrixMul4x4(
|
||||||
float xw = (float) x / (float)szer;
|
matrixMul4x4(
|
||||||
float yh = (float) y / (float) wys;
|
matrixMul4x4(
|
||||||
xw -= 0.5f;
|
matrixMul4x4(
|
||||||
yh -= 0.5f;
|
matrixMul4x4(
|
||||||
std::vector<float> P1 = {xw, yh, 1};
|
scale_mat,
|
||||||
std::vector<float> P2 = matrixMul(P1, tansform_mat);
|
rotation_mat_x),
|
||||||
P2[0] += 0.5f;
|
rotation_mat_y),
|
||||||
P2[1] += 0.5f;
|
rotation_mat_z),
|
||||||
DrawPixel(img, x, y, GetInterpolatedColor(P2[0], P2[1]));
|
sh_mat),
|
||||||
}
|
translate_mat);
|
||||||
|
|
||||||
|
for(int i = 1; i < 8; i++){
|
||||||
|
Point P1 = points[i-1];
|
||||||
|
Point P2 = points[i];
|
||||||
|
std::vector<float> p1_vec = GetXY(P1, std::vector<float>{});
|
||||||
|
std::vector<float> p2_vec = GetXY(P2, std::vector<float>{});
|
||||||
|
DrawLine(p1_vec[0], p1_vec[1], p2_vec[0], p2_vec[1], img);
|
||||||
|
}
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
QColor MyWindow::GetPixel(QImage* img, int x, int y){
|
QColor MyWindow::GetPixel(QImage* img, int x, int y){
|
||||||
QColor res;
|
QColor res;
|
||||||
int width = img->width();
|
int width = img->width();
|
||||||
@ -275,6 +327,40 @@ void MyWindow::paintEvent(QPaintEvent*)
|
|||||||
p.drawImage(poczX,poczY,*img);
|
p.drawImage(poczX,poczY,*img);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MyWindow::DrawLine(int x1, int y1, int x2, int y2, QImage *img){
|
||||||
|
if(x1 > x2){
|
||||||
|
std::swap(x1, x2);
|
||||||
|
std::swap(y1, y2);
|
||||||
|
}
|
||||||
|
float diff = x2 - x1;
|
||||||
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
||||||
|
|
||||||
|
QColor color(255, 255, 255, 255);
|
||||||
|
|
||||||
|
if(abs(a) < 1.0f){
|
||||||
|
for(int x = x1; x <= x2; x++){
|
||||||
|
int x_form = x - x1;
|
||||||
|
int y = a * x_form + y1;
|
||||||
|
|
||||||
|
DrawPixel(img, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(y1 > y2){
|
||||||
|
std::swap(x1, x2);
|
||||||
|
std::swap(y1, y2);
|
||||||
|
}
|
||||||
|
float diff = x2 - x1;
|
||||||
|
float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX;
|
||||||
|
for(int y = y1; y <= y2; y++){
|
||||||
|
int y_form = y - y1;
|
||||||
|
int x = ((float)(y_form) / a) + x1;
|
||||||
|
|
||||||
|
DrawPixel(img, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){
|
void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){
|
||||||
if(x >= szer || y >= wys || x < 0 || y < 0)
|
if(x >= szer || y >= wys || x < 0 || y < 0)
|
||||||
|
@ -31,6 +31,33 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <float.h> // for float,double macros
|
#include <float.h> // for float,double macros
|
||||||
|
|
||||||
|
struct Point{
|
||||||
|
float x, y, z;
|
||||||
|
|
||||||
|
Point(int x, int y, int z){
|
||||||
|
this->x=x;
|
||||||
|
this->y=y;
|
||||||
|
this->z=z;
|
||||||
|
}
|
||||||
|
|
||||||
|
float length(){
|
||||||
|
float mag = std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
|
||||||
|
return mag;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator-(const Point& other){
|
||||||
|
return Point(this->x - other.x, this->y - other.y, this->z - other.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator*(const float mul){
|
||||||
|
return Point(this->x * mul, this->y * mul, this->z * mul);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point operator+(const Point& other){
|
||||||
|
return Point(this->x + other.x, this->y + other.y, this->z + other.z);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
enum Mode {Line, Circle, Fill};
|
enum Mode {Line, Circle, Fill};
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
@ -88,7 +115,16 @@ private:
|
|||||||
int segment_count;
|
int segment_count;
|
||||||
|
|
||||||
float rotation_angle;
|
float rotation_angle;
|
||||||
Point[8] points;
|
Point points[8] = {
|
||||||
|
Point(-1.0f, -1.0f, 0.0f),
|
||||||
|
Point(1.0f, -1.0f, 0.0f),
|
||||||
|
Point(1.0f, 1.0f, 0.0f),
|
||||||
|
Point(-1.0f, 1.0f, 0.0f),
|
||||||
|
Point(-1.0f, -1.0f, 0.0f),
|
||||||
|
Point(1.0f, -1.0f, 0.0f),
|
||||||
|
Point(1.0f, 1.0f, 0.0f),
|
||||||
|
Point(-1.0f, 1.0f, 0.0f),
|
||||||
|
};
|
||||||
std::vector<float> scale_vec;
|
std::vector<float> scale_vec;
|
||||||
std::vector<float> translation_vec;
|
std::vector<float> translation_vec;
|
||||||
std::vector<float> sh_vec;
|
std::vector<float> sh_vec;
|
||||||
@ -113,10 +149,12 @@ private:
|
|||||||
void DrawS(float s);
|
void DrawS(float s);
|
||||||
void DrawV(float v);
|
void DrawV(float v);
|
||||||
|
|
||||||
|
void DrawLine(int x1, int y1, int x2, int y2, QImage *img);
|
||||||
QColor GetPixel(QImage* img, int x, int y);
|
QColor GetPixel(QImage* img, int x, int y);
|
||||||
void DrawPixel(QImage* img, int x, int y, QColor color);
|
void DrawPixel(QImage* img, int x, int y, QColor color);
|
||||||
QColor GetInterpolatedColor(float x, float y);
|
QColor GetInterpolatedColor(float x, float y);
|
||||||
void UpdateImage();
|
void UpdateImage();
|
||||||
|
std::vector<float> GetXY(Point p, std::vector<float> m);
|
||||||
|
|
||||||
// Deklaracje slotow, czyli funkcji wywolywanych
|
// Deklaracje slotow, czyli funkcji wywolywanych
|
||||||
// po wystapieniu zdarzen zwiazanych z GUI
|
// po wystapieniu zdarzen zwiazanych z GUI
|
||||||
|
Loading…
Reference in New Issue
Block a user