Download Types of Polygon

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assignment No:
Title: Polygon drawing in Qt
Problem Statement: Write a C++ program to draw a simple polygons (Square, Rectangle,
Triangle)
Theory:
Polygon : It is a closed polyline.
Types of Polygon :
1. Convex Polygon : A convex polygon is a polygon such that for any two points inside
the polygon, all points on the line segment connecting them are also inside the Polygon.
2. Concave Polygon : A concave polygon is a polygon such that for any two points inside
the polygon, if some points on the line segment connecting them are not inside the
Polygon.
Mathematical Model :
Assignment No:
Title: Polygon drawing in Java
Problem Statement: Write a Java program to draw
Theory:
polygon with programmable edges.
Polygon : It is a closed polyline.
Types of Polygon :
3. Convex Polygon : A convex polygon is a polygon such that for any two points inside
the polygon, all points on the line segment connecting them are also inside the Polygon.
4. Concave Polygon : A concave polygon is a polygon such that for any two points inside
the polygon, if some points on the line segment connecting them are not inside the
Polygon.
Graphics in java : To do custom graphics in a JAVA application, write a new class that extends
the JPanel class. In that class, override the definition of the paintComponent() method.
A Custom Graphics Template:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.swing.event.*;
public class ClassName extends JPanel {
public void paintComponent(Graphics g) {
}
super.paintComponent(g);
}
Java Swing:
It is java framework. Java Swing is a lightweight Java graphical user interface (GUI) widget
toolkit that includes a rich set of widgets. It includes several packages for developing rich
desktop applications in Java. Swing includes built-in controls such as trees, image buttons,
tabbed panes, sliders, toolbars, color choosers, tables, and text areas to display HTTP or rich text
format (RTF).
Scanner class simplifies console input. The Scanner class is a class in java.util, which allows
the user to read values of various type.
1. import java.awt.* : AWT stands for Abstract Window ToolKit. The Abstract Window
Toolkit provides many classes for programmers to use. It is the connection between the
application and the GUI. It contains classes that programmers can use to make graphical
components, e.g., buttons, labels, frames
2. import.java.util.* : The java.util package contains classes that deal with collections,
events, date and time, internationalization and various helpful utilities.
3. Import.java.swing.* : Swing is built on top of AWT, and provides a new set of more
sophisticated graphical interface components.
4. Import.java.swing.event.* : This package defines classes and interfaces used for event
handling in the AWT.
Here Jpanel is the canvas on which drawing is done.
paintComponent() method is pre-defined. If we want to draw something on the panel, then you
need to override it.
A Graphics object g controls the visual appearance of a Swing component.
Graphics object is obtained as a parameter to the paintComponent() method. Once you have the
Graphics object, you can send it messages to change the color or font that it uses, or to draw a
variety of geometric figures.
super.paintComponent(g) invokes the paintComponent method from the superclass of JPanel
(the JComponent class) to erase whatever is currently drawn on the panel. This is useful for
animation.
JFrame:
A Frame is a top-level window with a title and a border. A frame, implemented as an instance of
the JFrame class, is a window that has decorations such as a border, a title, and supports button
components that close or magnify the window. Applications with a GUI usually include at least
one frame.
Mathematical Model : A polygon is represented mathematically as :
polygon =
{line <pi, pi+1> | pi = <xi, yi> and pi+1 = <xi+1,yi+1> } where 1<=i<n
{ line <pn ,p1 | pn = <xn, yn> and p1 = <x1,y1> }
Assignment No:
Title: Polygon filling in Java
Problem Statement: Write a Java program to fill polygon using Scan line polygon filling
algorithm.
Theory:
Polygon filling Algorithms:
1. Flood fill
2. Boundary fill
3. Scan line polygon filling
Scan line polygon filling algorithm :
Advantages:
1. Faster
2. Less memory needed
Algorithm :
For each scan line:
1. Find the intersections of the scan line with all edges of the polygon.
2. Sort the intersections by increasing x-coordinate.
3. Fill in all pixels between pairs of intersections by line command .
Mathematical Model of Polygon filling :
n->no. of vertices of the polygon
{line <pi, pi+1> | pi = <xi, yi> and pi+1 = <xi+1,yi+1> } where 1<=i<n
Filled polygon =
{ line <pn ,p1 | pn = <xn, yn> and p1 = <x1,y1> }
And P(x,y) = 1 if P lies inside polygon
=0
otherwise
Title: Booths multiplication
Problem statement: Write a class to implement the Booths Multiplier for 8/16/32/64-bit
numbers using sign extended multiplication.
Theory:
Points to remember:
•
When using Booth's Algorithm:
– You will need twice as many bits in your product as you have in your original
two operands.
– The leftmost bit of your operands (both your multiplicand and multiplier) is a
SIGN bit, and cannot be used as part of the value.
To begin:
•
Decide which operand will be the multiplier and which will be the multiplicand
•
Convert both operands to two's complement representation using X bits
– X must be at least one more bit than is required for the binary representation of
the numerically larger operand
•
Begin with a product that consists of the multiplier with an additional X leading zero bits
Algorithm:
Step 1: Load A=0, Q-1 = 0
B = Multiplicand
Q = Multiplier and SC = n
Step 2: Check the status of Q0Q-1
if Q0Q-1= 10 perform A = A-B
if Q0Q-1= 01 perform A = A+B
Step 3: Arithmetic shift right : A,Q,Q-1
Step 4: Decrement sequence counter
if not zero, repeat step 2 through 4
Step 5: Stop
Flowchart:
Mathematical Model :
Assignment No:
Title: Inside Test of polygon
Problem Statement: Write a program in C++ (in QT) to test that given point is inside the
polygon.
Theory :
Polygon Inside test is used to check whether a point is inside or outside of the polygon. Methods
1. Even –odd method
2. Winding no. method
1. Even-odd Method :
P
Q1
P
Figure 1: polygon with Q1 inside and Q2 outside
 Count number of intersections with polygon edges
 If N is odd, point is inside
 If N is even, point is outside
Figure 1: intersection point is vertex
Q2
If intersection point is the vertex : In an inside test if intersections point is a vertex and edges
meeting at that vertex lies on same side of constructed horizontal line then the number of
intersections are considered as even else intersection point is considered as odd number.
Algorithm :
1.
2.
3.
4.
5.
6.
7.
8.
Accept no of polygon vertices (n)
Accept co-or of each polygon vertex x[ ] , y[ ]
Draw polygon
Accept a point for inside test (x,y)
Consider any point outside the polygon (0,y)
Draw a scan line between (0,y) and (x,y)
Count the no. of intersections made by line in step no. 6
If no. of intersection are odd point (x,y) is inside else it is outside (handle special cases of
intersection if intersection is a vertex )
Mathematical model :
P(x , y) = inside
if
= outside
if
Σ Inter = odd
Σ Inter
= even
Where Inter = no. of intersections made by the constructed horizontal line with polygon edges.
Tittle : Line Drawing using VRML code.
Problem statement : Use VRML to draw a line Diagram
Theory :
Introduction: VRML :Virtual Reality Modelling Language, is pronounced as ‘vermil’.
VRML is a scene description language.It is a standard for delivering 3D rendering on the net,
just like HTML is a standard for web pages and have ‘.wrl’ extension.
Though VRML is a computer language, it is not a programming language. VRML files are not
compiled, but are simple ascii / utf8 text files which can be parsed by a VRML interpreter.
VRML Browser : To see VRML worlds , we need to install a VRML browser (or player) like
'cosmoplayer'. Internet Explorer comes with a default VRML browser, and almost all other
internet browsers can install one.
VRML editors : VRML can be edited by any text editor. There are many available VRML
editors like VRMLpad .
The objects in a VRML file are called nodes.
Nodes are basically of three types:



Shape nodes, which describe actual geometry,
Property nodes, which modify geometry and
Grouping nodes, which cause groups of objects to be modified as one object.
A VRML browser has to be able to recognize a file as being VRML code in order to parse it.
This is accomplished with a simple file header string at the beginning of the file:
#VRML V2.0 utf8
Each VRML file must start with this comment line:
Note that, though in all other cases the pound symbol (#) indicates a comment
VRML assumes the following values for units of measurement:
Distance and size: meters
Angles: radians
Other values: percentage (expressed as fractions of 1)
VRML world is made out of nodes, which are types of objects.
Inside the nodes there are fields which are properties of the node and have a Default.
A node structure is
Transformations nodes :
Special terms used to describe the functions performed by the transformation nodes are
translate: change the location of the object's center,
rotate: change the orientation of the object's axes, and
scale: change the dimensions of the object.
Nodes can be nested:
A simple example
Shape {
geometry
}
Box { }
Instead of building complex objects from a lot of tiny primitives, objects can be using points,
lines, and faces;
To build objects using lines, points and faces need to describe geometry in two steps:
1. Define location of "vertices" (invisible dots) in the 3D space on which you can "tighten"
lines or facets: the vertices will be the "skeleton" of our geometry.
2. Build the "body of geometry" by connecting the vertices with lines or faces.
Define the location of dots (points) using Coordinate node ( each point is described by 3
coordinates):
Coordinate {
point [
2.0 1.0 3.0,
4.0 2.5 5.3,
...
]
}
Second connect the locations described by the immaterial/invisible dots by using one of the 3
nodes that have a coord field which accepts a Coordinate node
The PointSet node :
The PointSet node specifies a set of 3D points in the local coordinate system, with associated
colours at each point(but with no control over the sizes of the dots). The PointSet node and the
default values are:
PointSet {
color
NULL
coord NULL
}
The coord field specifies a Coordinate node : the specified coordinates are used in order by the
PointSet node. If the coord field is NULL(as it is default), the point set is considered empty.
Since the size of the points are implementation-dependent, the only thing what you can specify is
the color of each point in order. If the color field is NULL and there is a Material node defined
for the Appearance node affecting this PointSet node, the emissiveColor of the Material node
shall be used to draw the points.
Assignment No:
Title: Scan fill polygon for Android Mobile Programming.
Problem Statement: Write a program to scan fill the given concave polygon for Android
Mobile Programming. Use Android ADT for Eclipse.
Theory:
Android :
Android is an open source and Linux-based Operating System for mobile devices such as
smartphones and tablet computers.
Generally, Android is a software stack for mobile devices that includes an operating system,
middleware and key applications. Android is based on JAVA and all its applications are
developed in JAVA
Android Features:
 Application framework enabling reuse and replacement of components
 Optimized Java virtual machine: Dalvik
 Optimized Graphics Processing, supporting 2D and 3D graphics(OpenGL ES 1.0 )
 Integrated open source web browser: WebKit
 SQLite for structured data storage
Dalvik Virtual Machine: Android custom implementation virtual machine.
DVM vs. JVM :
 DVM
 Google
 Dalvik executable
 Only supports a subset of standard Java Library
 JVM
 Sun
 Java bytecode
Hello World Project : When you create first program in Android let us say Hello World
Project :


src: source folder
gen: SDK generated file

android 2.2: reference lib

assets: binary resources

res: resource files and resource description files

AndroidManifest.xml: application description file. Contains characteristics about your
application

default.properties: project properties file
Activities
o The basis of android applications. A single Activity defines a single viewable
screen. It can have multiple per application. Each activity is a separate entity.
They have a structured life cycle. Different events in their life happen either via
the user touching buttons or programmatically
Graphics in Android Programming:
Android comes along with powerful open-source API libraries which support custom 2D and
3D graphics as well animations.
There are two ways of implementation using these API.


Drawing to a View
Drawing on a Canvas
Drawing a circle to View
 Drawing to view is a better option when User Interface does not require dynamic
changes in the application. The most convenient aspect of doing so is that the Android
framework will provide a pre-defined Canvas to which drawing calls will be placed.

This can be achieved simply by extending the View class and define an onDraw()
callback method. Inside View component's onDraw(), use the Canvas given to the
application for all applications drawing, using various Canvas.draw...() methods
(Ex: canvas.drawCircle(x / 2, y / 2, radius, paint);).
onDraw() is a callback method invoked when the view is initially drawn.
Drawing rectangle on a canvas
To draw dynamic 2D graphics where in application needs to regularly re draw itself,
drawing on a canvas is a better option. A Canvas works as an interface, to the actual surface
upon which applications graphics will be drawn.
Assignment No:
Title: Line Styles in Python
Problem Statement: Write a Python program to draw a line with line styles.
Theory:
Introduction:
Python is a powerful modern computer programming language. Python allows you to use
variables without declaring them (i.e., it determines types implicitly), and it relies on indentation
as a control structure. Programmer is not forced to define classes in Python (unlike Java) but
he/she is free to do so when convenient. With python, small project can be written quickly.
Features of Python:
1. Simple: Python is a simple and minimalistic language. With python’s clear and simple
rules, python is closer to English.
2. It is Powerful.
3. Free and Open Source:
4. High-level Language: When you write programs in Python, you never need to bother
about the low-level details such as managing the memory used by your program, etc
5. Portable: Due to its open-source nature, Python has been ported (i.e. changed to make it
work on) to many platforms. You can use Python on Linux, Windows, FreeBSD,
Macintosh, Solaris etc.
6. Interpreted: Python, does not need compilation to binary. Internally, Python converts the
source code into an intermediate form called bytecodes and then translates this into the
native language of computer and then runs it.
7. Object Oriented: Python supports procedure-oriented programming as well as objectoriented programming. In procedure-oriented languages, the program is built around
procedures or functions which are nothing but reusable pieces of programs. In objectoriented languages, the program is built around objects which combine data and
functionality.
2. Why Python?
Java v/s Python :
1. Python programs run slower than the Java codes, but python saves much time and space.
Python programs are 3-5 times smaller than java programs.
2. Python is dynamic typed language. Python programmers don't need to waste time in
declaring variable types as in java.
3. Python is much easier to learn than Java.
C++ v/s Python:
1. Comparison is same as that between Java and Python except the program length in
python is 5-10 times shorter than that in C++.
2. Python programmers can complete a task in 2 months that takes a year in C++.
4. GUI Programming in Python
Python has a huge number of GUI frameworks (or toolkits) available for it. The major crossplatform technologies upon which Python frameworks are based include Gtk, Qt, Tk and
wxWidgets, although many other technologies provide actively maintained Python bindings.
Some of Python frameworks are
1. JPython: Jython is an implementation of the high-level, dynamic, object-oriented language
Python seamlessly integrated with the Java platform. It can be fully integrated into
existing Java applications; alternatively, Python applications can be compiled into a
collection of Java classes. Python programs running on the Jython virtual machine have
full access to the Java classes and APIs.
2. PyQt: PyQt is a Python binding of the cross-platform GUI toolkit Qt. It is one of Python's
options for GUI programming. PyQt is free software. PyQt is implemented as a
Python plug-in.
3. PyGTK: PyGTK is a set of Python wrappers for the GTK+ graphical user
interface library. PyGTK is free software.
4. PyCairo:
5. Tkinter: Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python
interface to the Tk GUI toolkit and is Python's de facto standard GUI, and is included with
the standard Windows and Mac OS X install of Python. The name Tkinter comes from Tk
interface. Tkinter is implemented as a Python wrapper around a complete Tcl interpreter
embedded in the Python interpreter. Tkinter calls are translated into Tcl commands which
are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a
single application.
Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a
fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented
interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the
following steps:
a)
b)
c)
d)
Import the Tkinter module.
Create the GUI application main window.
Add one or more of the above-mentioned widgets to the GUI application.
Enter the main event loop to take action against each event triggered by the user.
Example
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
This would create a following window:
Tkinter Widgets
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI
application. These controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. We present these widgets as well as a brief
description in the following table:
Operator
Description
Button
The Button widget is used to display buttons in your application.
Canvas
The Canvas widget is used to draw shapes, such as lines, ovals,
polygons and rectangles, in your application.
Checkbutton
The Checkbutton widget is used to display a number of options as
checkboxes. The user can select multiple options at a time.
Entry
The Entry widget is used to display a single-line text field for
accepting values from a user.
Frame
The Frame widget is used as a container widget to organize other
widgets.
Label
The Label widget is used to provide a single-line caption for other
widgets. It can also contain images.
Listbox
The Listbox widget is used to provide a list of options to a user.
Menubutton
The Menubutton widget is used to display menus in your application.
Menu
The Menu widget is used to provide various commands to a user.
These commands are contained inside Menubutton.
Message
The Message widget is used to display multiline text fields for
accepting values from a user.
Radiobutton
The Radiobutton widget is used to display a number of options as
radio buttons. The user can select only one option at a time.
Scale
The Scale widget is used to provide a slider widget.
Scrollbar
The Scrollbar widget is used to add scrolling capability to various
widgets, such as list boxes.
Text
The Text widget is used to display text in multiple lines.
Toplevel
The Toplevel widget is used to provide a separate window container.
Spinbox
The Spinbox widget is a variant of the standard Tkinter Entry
widget, which can be used to select from a fixed number of values.
PanedWindow
A PanedWindow is a container widget that may contain any number
of panes, arranged horizontally or vertically.
LabelFrame
A labelframe is a simple container widget. Its primary purpose is to
act as a spacer or container for complex window layouts.
tkMessageBox This module is used to display message boxes in your applications.
Geometry Management in Tkinter:
All Tkinter widgets have access to specific geometry management methods, which have the
purpose of organizing widgets throughout the parent widget area. Tkinter exposes the following
geometry manager classes: pack, grid, and place.



The pack() Method - This geometry manager organizes widgets in blocks before placing them in
the parent widget.
The grid() Method - This geometry manager organizes widgets in a table-like structure in the
parent widget.
The place() Method -This geometry manager organizes widgets by placing them in a specific
position in the parent widget.
The Canvas Widget:
The canvas widget provides the basic graphics facilities for Tkinter, and so more advanced
functions. Drawing on the canvas is done by creating various items on it.
The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You
can place graphics, text, widgets or frames on a Canvas.
Syntax:
Here is the simple syntax to create this widget:
w = Canvas ( master, option=value, ... )


Parameters:
master: This represents the parent window.
options: Here is the list of most commonly used options for this widget. These options can be
used as key-value pairs separated by commas.
Option
Description
Bd
Border width in pixels. Default is 2.
Bg
Normal background color.
Confine
If true (the default), the canvas cannot be scrolled outside of the
scrollregion.
Cursor
Cursor used in the canvas like arrow, circle, dot etc.
Height
Size of the canvas in the Y dimension.
highlightcolor
Color shown in the focus highlight.
Relief
Relief specifies the type of the border. Some of the values are
SUNKEN, RAISED, GROOVE, and RIDGE.
Scrollregion
A tuple (w, n, e, s) that defines over how large an area the canvas
can be scrolled, where w is the left side, n the top, e the right side,
and s the bottom.
Width
Size of the canvas in the X dimension.
If you set this option to some positive dimension, the canvas can be
positioned only on multiples of that distance, and the value will be
xscrollincrement
used for scrolling by scrolling units, such as when the user clicks
on the arrows at the ends of a scrollbar.
xscrollcommand
If the canvas is scrollable, this attribute should be the .set() method
of the horizontal scrollbar.
yscrollincrement Works like xscrollincrement, but governs vertical movement.
yscrollcommand
If the canvas is scrollable, this attribute should be the .set() method
of the vertical scrollbar.
The Canvas widget can support the following standard items:
arc . Creates an arc item, which can be a chord, a pieslice or a simple arc.
coord = 10, 50, 240, 210
arc = canvas.create_arc(coord, start=0, extent=150, fill="blue")
image . Creates an image item, which can be an instance of either the BitmapImage or the
PhotoImage classes.
filename = PhotoImage(file = "sunshine.gif")
image = canvas.create_image(50, 50, anchor=NE, image=filename)
line . Creates a line item.
id = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, option, ...)
The line goes through the series of points (x0, y0), (x1, y1), … (xn, yn).
Some Canvas line options are:
a) activedash, activefill, activestipple, activewidth : These options specify
the dash, fill, stipple, and width values to be used when the line is active, that is,
when the mouse is over it.
b) Arrow: The default is for the line to have no arrowheads. Use arrow=tk.FIRST to get an
arrowhead at the (x0, y0) end of the line. Use arrow=tk.LAST to get an arrowhead at the
far end. Usearrow=tk.BOTH for arrowheads at both ends.
c) Dash: This option is specified as a tuple of integers. The first integer specifies how many
pixels should be drawn. The second integer specifies how many pixels should be skipped
before starting to draw again, and so on. When all the integers in the tuple are exhausted,
they are reused in the same order until the border is complete.
For example, dash=(3,5) produces alternating 3-pixel dashes separated by 5-pixel gaps. A
value of dash=(7,1,1,1) produces a dash-and-dot pattern, with the dash seven times as
long as the dot or the gaps around the dot. A value of dash=(5,) produces alternating fivepixel dashes and five-pixel gaps.
d) dashoffset: To start the dash pattern in a different point of cycle instead of at the
beginning, use an option of dashoff=n, where n is the number of pixels to skip at the
beginning of the pattern.
e) fill : The color to use in drawing the line. Default is fill='black'.
f) width: The line's width. Default is 1 pixel.
oval . Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; the
top left and bottom right corners of the bounding rectangle for the oval.
oval = canvas.create_oval(x0, y0, x1, y1, options)
polygon . Creates a polygon item that must have at least three vertices.
polygon = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)
Assignment No:
Title: Bouncing Ball in Maya / 3D studio.
Problem Statement: Write a Program to show bouncing ball animation in 3D Studio.