Assignment 2: Write C++/Java program to draw inscribed and Circumcircled circles in triangle as shown below:
Program: The Program is created using QTCreator.

And consist of following files:

(click on these link to view)

  • mainwindow.cpp
 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include<math.h>

QImage image(411,391,QImage::Format_RGB888);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::draw_DDA(float r, float x, float y)
{
    float x1,y1,x2,y2,start_x,start_y,e;
    int i,val;

    x1=r;
    y1=0;
    start_x=x1;
    start_y=y1;

    i=0;
    do{
        val=pow(2,i);
        i++;
    }while(val<r);

    e=1/pow(2,i-1);

    do
    {
        x2=x1+y1*e;
        y2=y1-x2*e;
        image.setPixel(x+x2,y+y2,qRgb(255,255,255));
        x1=x2;
        y1=y2;
    }while(((y1-start_y)<e)||((start_x-x1)>e));

}

void MainWindow::draw_Bresenhams(float r, float x, float y)
{
    float x2,y2;
    float d;
    x2=0;
    y2=r;
    d=3-2*r;

    do{
        image.setPixel(x-y2,y-x2,qRgb(255,255,255));
        image.setPixel(x+y2,y-x2,qRgb(255,255,255));
        image.setPixel(x-y2,y+x2,qRgb(255,255,255));
        image.setPixel(x+y2,y+x2,qRgb(255,255,255));
        image.setPixel(x+x2,y+y2,qRgb(255,255,255));
        image.setPixel(x-x2,y+y2,qRgb(255,255,255));
        image.setPixel(x+x2,y-y2,qRgb(255,255,255));
        image.setPixel(x-x2,y-y2,qRgb(255,255,255));

        if(d<=0)
        {
            d=d+4*x2+6;
        }
        else
        {
            d=d+4*(x2-y2)+10;
            y2=y2-1;
        }
        x2=x2+1;
    }while(x2<y2);
}

QImage MainWindow::getImage()
{
    return image;
}

void MainWindow::draw_Triangle(float r,float cx,float cy)
{
    int p1=cx;
    int q1=cy-r;

    image.setPixel(p1,q1,qRgb(255,255,255));
    int p2=0,p3=0,q2=0,q3=0;

    float x1,y1,x2,y2,start_x,start_y,e;
    cy=cy+r;

    int i,val;
    x1=r;
    y1=0;
    start_x=x1;
    start_y=y1;

    i=0;

    do
    {
        val=pow(2,i);
        i++;
    }while(val<r);

    e=1/pow(2,i-1);

    int cho=0;
    do
    {
        x2=x1+y1*e;
        y2=y1-x2*e;
        QRgb value=image.pixel(cx+x2,cy+y2);


        if(value==qRgb(255,255,255))
        {
            if(cho==0)
            {
                cho++;
                p2=cx+x2;
                q2=cy+y2;
            }
            else
            {
                p3=cx+x2;
                q3=cy+y2;
            }
        }
        x1=x2;
        y1=y2;
    }while(((y1-start_y)<e)||((start_x-x1)>e));

   drawline(p1,q1,p2,q2);
   drawline(p2,q2,p3,q3);
   drawline(p1,q1,p3,q3);
}

int MainWindow::sign(int a)
{
    if(a<0)
        return -1;
    else if(a>0)
        return 1;
    else return 0;
}


void MainWindow::drawline(float x1,float y1,float x2,float y2)
{
    float dx=abs(x2-x1);
    float dy=abs(y2-y1);
    float x=x1;
    float y=y1;

    int s1=sign(x2-x1);
    int s2=sign(y2-y1);

    int in;

    if(dy>dx)
    {
        in=1;
        float t=dx;
        dx=dy;
        dy=t;
    }
    else
        in=0;

    float e=2*dy-dx;

    int i=1;

    while(i<=dx)
    {
        image.setPixel(x,y,qRgb(255,255,255));
        while(e>=0)
        {
            if(in==1)
                x=x+s1;
            else
                y=y+s2;
            e=e-2*dx;
        }
        if(in==1)
            y=y+s2;
        else
            x=x+s1;

        e=e+2*dy;
        i++;
    }
}


void MainWindow::on_pushButton_clicked()
{
    float r=ui->textEdit_r->toPlainText().toFloat();
    float cx=ui->textEdit_x->toPlainText().toFloat();
    float cy=ui->textEdit_y->toPlainText().toFloat();
    draw_DDA(r,cx,cy);
    QImage i=getImage();
    ui->label_4->setPixmap(QPixmap::fromImage(i));
    ui->pushButton_2->setEnabled(true);
    ui->pushButton_3->setEnabled(true);
}

void MainWindow::on_pushButton_2_clicked()
{
    float r=ui->textEdit_r->toPlainText().toFloat();
    float cx=ui->textEdit_x->toPlainText().toFloat();
    float cy=ui->textEdit_y->toPlainText().toFloat();
    draw_Bresenhams(r/2,cx,cy);
    QImage i=getImage();
    ui->label_4->setPixmap(QPixmap::fromImage(i));
}

void MainWindow::on_pushButton_3_clicked()
{
    float r=ui->textEdit_r->toPlainText().toFloat();
    float cx=ui->textEdit_x->toPlainText().toFloat();
    float cy=ui->textEdit_y->toPlainText().toFloat();
    draw_Triangle(r,cx,cy);
    QImage i=getImage();
    ui->label_4->setPixmap(QPixmap::fromImage(i));
}
  • main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

  • mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void draw_DDA(float,float,float);
    void draw_Bresenhams(float,float,float);
    void draw_Triangle(float,float,float);
    int sign(int);
    void drawline(float,float,float,float);
    QImage getImage();
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
  • mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>532</width>
    <height>471</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Inner Circle</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_r">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>90</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_x">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>30</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>190</y>
      <width>300</width>
      <height>300</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Triangle</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_y">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>30</y>
      <width>81</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>40</y>
      <width>46</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Center Y</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>150</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Outer Circle</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>40</y>
      <width>46</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Center X</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>100</y>
      <width>31</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>Radius</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>532</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


Output:




Comments

Popular posts from this blog