Skip to content

Commit

Permalink
electric field drawing added
Browse files Browse the repository at this point in the history
added some samples
  • Loading branch information
mmmoradi committed May 30, 2023
1 parent 92670f9 commit ed34400
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 25 deletions.
13 changes: 11 additions & 2 deletions src/charge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ void Charge::updatePosition()
velocity = velocity + acceleration * env.deltaT;

// TODO change the range for x and y
if (position.getX() < -300 || position.getX() > 300) {
// TODO correct the collision of the charge with the wall
if (position.getX() < -350 || position.getX() > 350) {
velocity.setX(-velocity.getX());
}

if (position.getY() < -300 || position.getY() > 300) {
if (position.getY() < -350 || position.getY() > 350) {
velocity.setY(-velocity.getY());
}
}
Expand All @@ -67,3 +68,11 @@ bool Charge::getSign() const
{
return q > 0 ? true : false;
}


Vector Charge::getElectricField(Vector point)
{
Vector r = point - this->position;

return r.normalize() * this->q / r.magnitudeSquerd();
}
2 changes: 2 additions & 0 deletions src/charge.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Charge

QPoint getPosition() const;
bool getSign() const;

Vector getElectricField(Vector point);
};

#endif // CHARGE_H
68 changes: 55 additions & 13 deletions src/coulosim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@ Coulosim::~Coulosim()

void Coulosim::on_btn_add_charge_clicked()
{
int px = ui->doubleSpinBox_px->value();
int py = ui->doubleSpinBox_py->value();

int vx = ui->doubleSpinBox_vx->value();
int vy = ui->doubleSpinBox_vy->value();

float q = ui->doubleSpinBox_q->value();

float mass = ui->doubleSpinBox_mass->value();

bool mobile = ui->checkBox_mobile->isChecked();

ui->renderArea->addCharge(px, py, vx, vy, q, mass, mobile);
ui->renderArea->addCharge(ui->doubleSpinBox_px->value(),
ui->doubleSpinBox_py->value(),
ui->doubleSpinBox_vx->value(),
ui->doubleSpinBox_vy->value(),
ui->doubleSpinBox_q->value(),
ui->doubleSpinBox_mass->value(),
ui->checkBox_mobile->isChecked()
);
}


Expand All @@ -55,3 +50,50 @@ void Coulosim::on_pushButton_reset_clicked()
{
ui->renderArea->reset();
}


void Coulosim::on_pushButton_sample_1_clicked()
{
ui->doubleSpinBox_electric_x->setValue(0);
ui->doubleSpinBox_electric_y->setValue(0);
ui->doubleSpinBox_magnetic->setValue(0);
ui->doubleSpinBox_speed->setValue(1);

ui->renderArea->addCharge(0,0,0,0,0.001,1,0);
ui->renderArea->addCharge(-50,20,15,0,-0.001,1,1);
}


void Coulosim::on_pushButton_sample_2_clicked()
{
ui->doubleSpinBox_electric_x->setValue(0.10);
ui->doubleSpinBox_electric_y->setValue(0);
ui->doubleSpinBox_magnetic->setValue(0);
ui->doubleSpinBox_speed->setValue(1);

ui->renderArea->addCharge(0,0,0,0,-0.001,0.000001,1);
}


void Coulosim::on_pushButton_sample_3_clicked()
{
ui->doubleSpinBox_electric_x->setValue(0);
ui->doubleSpinBox_electric_y->setValue(0);
ui->doubleSpinBox_magnetic->setValue(-0.01);
ui->doubleSpinBox_speed->setValue(10);

ui->renderArea->addCharge(0,0,1,0,0.000001,0.000001,1);
}


void Coulosim::on_pushButton_sample_4_clicked()
{
ui->doubleSpinBox_electric_x->setValue(0);
ui->doubleSpinBox_electric_y->setValue(0);
ui->doubleSpinBox_magnetic->setValue(0);
ui->doubleSpinBox_speed->setValue(0.01);

ui->renderArea->addCharge(0,0,0,0,-1,1,0);
ui->renderArea->addCharge(0,100,9480,0,1,1,1);
}

8 changes: 8 additions & 0 deletions src/coulosim.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ private slots:

void on_pushButton_reset_clicked();

void on_pushButton_sample_1_clicked();

void on_pushButton_sample_2_clicked();

void on_pushButton_sample_3_clicked();

void on_pushButton_sample_4_clicked();

private:
Ui::Coulosim *ui;
};
Expand Down
72 changes: 67 additions & 5 deletions src/coulosim.ui
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
<rect>
<x>820</x>
<y>10</y>
<width>321</width>
<width>353</width>
<height>781</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="5" column="0">
<spacer name="verticalSpacer">
<item row="2" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
Expand All @@ -47,7 +47,7 @@
</property>
</spacer>
</item>
<item row="0" column="0">
<item row="1" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
Expand Down Expand Up @@ -178,7 +178,51 @@
</item>
</layout>
</item>
<item row="0" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="8" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QPushButton" name="pushButton_sample_1">
<property name="text">
<string>sample 1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_sample_2">
<property name="text">
<string>sample 2</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="7" column="0">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_8">
Expand Down Expand Up @@ -327,7 +371,7 @@
</item>
</layout>
</item>
<item row="3" column="0">
<item row="6" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -340,6 +384,24 @@
</property>
</spacer>
</item>
<item row="5" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_13">
<item>
<widget class="QPushButton" name="pushButton_sample_3">
<property name="text">
<string>sample 3</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_sample_4">
<property name="text">
<string>sample 4</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
Expand Down
28 changes: 23 additions & 5 deletions src/renderarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ RenderArea::RenderArea(QWidget *parent)
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
isPlay = false;
elapsed = 0;
addCharge(0,0,0,0,0.001,1,0);
addCharge(-50,20,15,0,-0.001,1,1);
// addCharge(0,0,0,0,-0.001,1,1);
}


Expand Down Expand Up @@ -80,7 +77,6 @@ void RenderArea::reset()
}


// TODO add vector field
void RenderArea::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
Expand All @@ -89,7 +85,7 @@ void RenderArea::paintEvent(QPaintEvent *event)
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(event->rect(), QBrush(Qt::white));

painter.drawText(8,15, QString::number(elapsed)); //DEBUG
painter.drawText(8,15, QString::number(elapsed) + " ms"); //DEBUG
QElapsedTimer t;
t.start();
// Transformations based on real world cordinate system
Expand All @@ -103,6 +99,7 @@ void RenderArea::paintEvent(QPaintEvent *event)
updateCharges();

drawCharges(painter);
drawElectricField(painter);

elapsed = t.elapsed();
}
Expand Down Expand Up @@ -141,6 +138,27 @@ void RenderArea::drawCharges(QPainter& painter)
}


void RenderArea::drawElectricField(QPainter &painter)
{
for (int i = -350; i <= 350; i += 20) {
for (int j = -350; j <= 350; j += 20) {
Vector ef = {0,0};

for (auto& charge : env.listOfCharge) {
ef = charge.getElectricField(Vector(i,j)) + ef;
}
ef = ef + env.electricField;
ef = ef.normalize();
ef = ef *10;
// painter.setPen(QPen(Qt::darkGray,3,Qt::SolidLine,Qt::RoundCap));
// painter.drawPoint(i, j);
painter.setPen(Qt::darkGray);
painter.drawLine(i,j,i+ef.getX(),j+ef.getY());
}
}
}


void RenderArea::updateCharges()
{
for (auto& charge : env.listOfCharge) {
Expand Down
1 change: 1 addition & 0 deletions src/renderarea.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RenderArea : public QWidget
private:
void drawCoordinates(QPainter& painter);
void drawCharges(QPainter& painter);
void drawElectricField(QPainter& painter);
void updateCharges();


Expand Down

0 comments on commit ed34400

Please sign in to comment.