Computer Graphics & Visual Basic Lab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

END TERM PRACTICAL EXAMINATION, JUNE, 2016

BACHELOR OF COMPUTER APPLICATIONS

COMPUTER GRAPHICS & VISUAL BASIC LAB


___________________________________________________________________________________

Note: Attempt all questions


Q1. Write a program in VB to implement a simple calculator.
' Hold the left and right values
' of the calculation
Dim dblLeft As Double
Dim dblRight As Double

' Tracks where in the calculation progress we are


Dim intInProg As Integer

' Stores the current operator selected


Dim intOperator As Integer

' Store the previous operator selected


Dim intPrevOperator As Integer

' Indicates when the text field should be cleared


Dim intClearText As Integer

Private Sub Calculate()

' Check to see if we are just getting started with


' the first numbers entered for this calculation
If intInProg = 0 Then

' Get the number entered


dblLeft = txtResults.Text

' Set the next stage


intInProg = 1

' Clear the text box


txtResults.Text = ""

' Store the current operator as the previous


' one clicked
intPrevOperator = intOperator

Exit Sub

Else

' Get the right hand side of the operation


dblRight = txtResults.Text

' Clear the text box


txtResults.Text = ""

End If

' Perform the last operation


Select Case intPrevOperator

Case 1 ' add


dblLeft = dblLeft + dblRight
dblRight = 0
txtResults.Text = dblLeft

Case 2 ' subtract


dblLeft = dblLeft - dblRight
dblRight = 0
txtResults.Text = dblLeft

Case 3 ' multiply


dblLeft = dblLeft * dblRight
dblRight = 0
txtResults.Text = dblLeft

Case 4 ' divide


' Check for a divide by 0 error
On Error GoTo DivideZero
dblLeft = dblLeft / dblRight
dblRight = 0
txtResults.Text = dblLeft

End Select

' Take the current operator as the previous operator


' Next time we calc we will need to perform the operation
' that fired off this calculation. Note we still need the
' value on the right of the operator
intPrevOperator = intOperator

' Indicate the text field should be cleared


intClearText = 1

' Exit the subroutine


Exit Sub

' Divide by Zero Support


DivideZero:

' Notify the user


MsgBox "You can not divide a number by 0."

' Clear the calculator


cmdClear_Click

End Sub

Private Sub cmdClear_Click()

' Clear all values


txtResults.Text = ""
dblLeft = 0
dblRight = 0
intOperator = 0
intPrevOperator = 0
intInProg = 0
intClear = 0

End Sub

Private Sub cmdDecimal_Click()

' Add a decimal to the value


txtResults.Text = txtResults.Text & "."

End Sub

Private Sub cmdDivide_Click()

' Indicate the operator


intOperator = 4

' Make the calculation


Calculate

End Sub

Private Sub cmdEqual_Click()

' Indicate the operator


intOperator = 5

' Make the calculation


Calculate

End Sub

Private Sub cmdMinus_Click()

' Indicate the operator


intOperator = 2

' Make the calculation


Calculate

End Sub

Private Sub cmdMultiply_Click()

' Indicate the operator


intOperator = 3

' Make the calculation


Calculate

End Sub
Private Sub cmdNumber_Click(Index As Integer)

' If set clear the text


If intClearText = 1 Then

' Clear the text


txtResults.Text = ""

' Reset the value


intClearText = 0

End If

' Check to see if our index is not 0


If Index <> 9 Then

' Set the number value


txtResults.Text = txtResults.Text & (Index + 1)

Else

' Add 0
txtResults.Text = txtResults.Text & "0"

End If

End Sub

Private Sub cmdPlus_Click()

' Inidicate the operator


intOperator = 1

' Make the calculation


Calculate

End Sub

Private Sub Form_Load()

' Start out at 0


intInProg = 0

End Sub
Q2. Write a program to draw circle using mid-point algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void plot_point(int,int,int,int);
void circle_mid_point(int,int,int);
void main()
{
int x_c,y_c,r;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Enter the coordinates of center of the circle : ");
scanf("%d%d",&x_c,&y_c);
printf("\n Enter the radius of circle : ");
scanf("%d",&r);
circle_mid_point(x_c,y_c,r);
getch();
}
void circle_mid_point(int x_c,int y_c,int r)
{
int x,y,p;
p=1-r;
x=0;
y=r;
while(x<y)
{
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
plot_point(x_c,y_c,x,y);
}
}
void plot_point(int x_c,int y_c,int x,int y)
{
putpixel((x_c+x),(y_c+y),5);
putpixel((x_c-x),(y_c+y),5);
putpixel((x_c+x),(y_c-y),5);
putpixel((x_c-x),(y_c-y),5);
putpixel((x_c+y),(y_c+x),5);
putpixel((x_c-y),(y_c+x),5);
putpixel((x_c+y),(y_c-x),5);
putpixel((x_c-y),(y_c-x),5);
}
Q3. Write a program to draw a line using Bresenhams algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void line_bres(int,int,int,int);

void main()
{
int x1, x2, y2, y1;
int gd=DETECT, gm;
clrscr();

printf("\n Enter the end coordinates of the line: ");


printf("\n x1, y1: ");
scanf("%d%d", &x1, &y1);
printf("\n x2, y2: ");
scanf("%d%d", &x2,&y2);

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line_bres(x1,y1,x2,y2);
getch();
}

void line_bres(int x1, int y1, int x2, int y2)


{
int dx, dy, x, y, x_end, p;

dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;

if(x1>x2)
{
x=x2;
y=y2;
x_end=x1;
}
else
{
x=x1;
y=y1;
x_end=x2;
}
putpixel(x,y,1);

while(x<x_end)
{
x+=1;

if(p<0)
{
p+=2*dy;
}
else
{
y+=1;
p+=2*(dy-dx);
}
putpixel(x,y,1);
}
}
Q4. Create a simple database in MS Access Database /Oracle and a simple database application in VB
that shows database connectivity through DAO and ADO.

' Click event of the add command


' button
Private Sub cmdAdd_Click()

' Add a new record to the record set


Data1.Recordset.AddNew

' Hide the add button


cmdAdd.Visible = False

' Display the Update button


cmdUpdate.Visible = True

End Sub

' Click event of the Update command


' button
Private Sub cmdUpdate_Click()

' Update the data control


Data1.Recordset.Update

' Make the add command button


' visible
cmdAdd.Visible = True
' Hide the update button
cmdUpdate.Visible = False

End Sub
Q5. Write a Program to draw a line of slope 1 Using DDA Algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

#define ROUND(a) ((int) (a+0.5))

void line_DDA(int,int,int,int);

void main()
{
int x1, x2, y2, y1;
int gd=DETECT, gm;
clrscr();

printf("\n Enter the end coordinates of the line: ");


printf("\n x1, y1: ");
scanf("%d%d", &x1, &y1);
printf("\n x2, y2: ");
scanf("%d%d", &x2,&y2);

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line_DDA(x1,y1,x2,y2);
getch();
}

void line_DDA(int x1, int y1, int x2, int y2)


{
int dx, dy, steps, k;
float xIncrement, yIncrement, x=x1, y=y1;
dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>dy)
steps=dx;
else
steps=dy;
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;

putpixel(ROUND(x), ROUND(y), 1);


for (k=0; k<steps; k++) {
x += xIncrement;
y += yIncrement;
putpixel(ROUND(x), ROUND(y), 3);
}
}

You might also like