#include <graphics.h>
#include <iostream>
#define ROUND(x) ((int)(x+0.5))
typedef int Point[2]; // P[0] - x coordinate P[1] - y coordinate
void seedFill(int x, int y, int color) {
if(x > 640 || x < 0 || y > 480 || y < 0)
return;
if(getpixel(x, y) == BLACK) {
putpixel(x, y, color);
seedFill(x+1, y, color);
seedFill(x-1, y, color);
seedFill(x, y+1, color);
seedFill(x, y-1, color);
}
}
void scanlineFill(Point *polygon, int nodes, int color) {
setcolor(color);
Point array[480];
for(int i=0; i<480; ++i) {
array[i][0] = 640;
array[i][1] = 0;
}
for(int i=0; i<nodes; ++i){
Point p1, p2;
p1[0] = polygon[i][0]; p1[1] = polygon[i][1];
p2[0] = polygon[(i+1)%nodes][0]; p2[1] = polygon[(i+1)%nodes][1];
if(p1[1] > p2[1]) {
p2[0] = polygon[i][0]; p2[1] = polygon[i][1];
p1[0] = polygon[(i+1)%nodes][0]; p1[1] = polygon[(i+1)%nodes][1];
}
double m = (double)(p2[1]-p1[1])/(p2[0]-p1[0]);
double xd = p1[0] - 1/m;
for(int y=p1[1]; y<=p2[1]; ++y) {
xd += 1/m;
int x = ROUND(xd);
if(array[y][0] > x)
array[y][0] = x;
if(array[y][1] < x)
array[y][1] = x;
}
}
for(int i=0; i<480; ++i)
if(array[i][0] < array[i][1])
line(array[i][0]+1, i, array[i][1]-1, i);
}
void drawPolygon(Point *polygon, int nodes) {
setcolor(WHITE);
for(int i=0; i<nodes; ++i){
Point p1, p2;
p1[0] = polygon[i][0]; p1[1] = polygon[i][1];
p2[0] = polygon[(i+1)%nodes][0]; p2[1] = polygon[(i+1)%nodes][1];
line(p1[0], p1[1], p2[0], p2[1]);
}
}
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\");
Point polygon[20];
int size = 0;
while(true) {
std::system("cls");
std::cout << "Enter size of polygon : (-1 for exit) : ";
std::cin >> size;
if(size == -1)
return 0;
if(size > 20){
std::cout << "Enter size less than 20" << std::endl;
continue;
}
for(int i=0; i < size; ++i) {
std::cout << "Enter vertex : (x y) : ";
std::cin >> polygon[i][0] >> polygon[i][1];
}
drawPolygon(polygon, size);
FILL:
std::system("cls");
std::cout << "Choose an algorithm to fill." << std::endl;
std::cout << "\t1. seed fill" << std::endl;
std::cout << "\t2. scan line fill" << std::endl;
std::cout << "choice: ";
int choice;
std::cin >> choice;
switch(choice) {
case 1:
{
std::cout << "Enter a point inside polygon: (x y) : ";
Point p;
std::cin >> p[0] >> p[1];
seedFill(p[0], p[1],RED);
break;
}
case 2:
scanlineFill(polygon, size, BLUE);
break;
default:
std::cout << "I can't interpret your choice." << std::endl;
goto FILL;
break;
}
}
}
Friday, December 11, 2009
Area Filling Algorithms (seed filling, ScanLine conversion)
Labels:
algorithms,
computer graphics,
current,
programming,
source
Tuesday, December 1, 2009
Eight Queens Solution with DFS using Backtracking (Step-by-step simulation)
- package eight_queen;
- import java.awt.AWTEvent;
- import java.awt.AlphaComposite;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Composite;
- import java.awt.EventQueue;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowEvent;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- /**
- *
- * @author rooparam
- */
- class Locations {
- public Locations(int size ) {
- at = new int[size];
- top = 0; // next coloumn of a queen
- }
- public int top;
- public int[] at;
- }
-
- MyButton( ) {
- super("continue");
- }
- }
-
- Board ( Locations Q ) {
- queens = Q;
- //setSize(640, 480);
-
- }
- @Override
-
-
- g.drawImage(image, 0, 0, getBounds().width, getBounds().height, this);
- drawBoard ( g );
- drawQueens(g);
- drawRemaining(g);
-
-
- g.drawString(str, getBounds().width - 180, getBounds().height - 10);
- }
- private Locations queens;
-
-
-
- float alpha = 0.8f;
-
-
-
- int width = getBounds( ).width;
- int height = getBounds( ).height;
- int w_Q = width / queens.at.length;
- int h_Q = height / queens.at.length;
- for(int col = queens.top; col < queens.at.length; ++col) {
- for(int row = 0; row < queens.at.length; ++row) {
- if( !Main.isSafe(queens, row, col) )
- g.fill3DRect(col*w_Q, row*h_Q, w_Q, h_Q, true);
- }
- }
- g.setComposite(oldC);
- }
-
- int width = getBounds( ).width;
- int height = getBounds( ).height;
- int w_Q = width / queens.at.length;
- int h_Q = height / queens.at.length;
- for(int col = 0; col < queens.top; ++col) {
- drawQueen(g, col*w_Q, queens.at[col]*h_Q, w_Q, h_Q);
- }
- }
-
-
-
- float alpha = 0.9f;
-
-
-
- int width = getBounds( ).width;
- int height = getBounds( ).height;
- int w_Q = width / queens.at.length;
- int h_Q = height / queens.at.length;
- for(int col = 0; col < queens.at.length; ++col) {
- for(int row = 0; row < queens.at.length; ++row) {
- //g.fillRect(col*w_Q, row*h_Q, w_Q, h_Q);
- g.fill3DRect(col*w_Q, row*h_Q, w_Q, h_Q, true);
-
- }
-
- }
- g.setComposite(oldC);
- }
-
-
- g.setColor(clr);
- else
-
- }
-
-
-
- g.fillArc ( x + width/8, y + 7*height/8, 3*width/4, height/4, 0, 180);
- int lines = 16;
- double xi = x + width/8.0;
- double yi = y + height/2.0;
- double step_x = 3.0*width/(4*lines);
- double step_y = height/(2.0*lines);
- g.drawLine((int)xi, (int)yi, (int)(xi+step_x), (int)( y+height-2) );
- for(int i=0; i < lines/2; ++i) {
- xi += step_x; yi -= step_y;
- g.drawLine((int)xi, (int)yi, (int)xi, (int)(y+height-2.0));
- }
- for(int i=1; i < lines/2; ++i) {
- xi += step_x; yi += step_y;
- g.drawLine((int)xi, (int)yi, (int)xi, (int)(y+height-2.0));
- }
- xi += step_x; yi += step_y;
- g.drawLine((int)xi, (int)yi, (int)(xi-step_x), (int)(y+height-2.0));
- g.setColor(oldPen);
- }
- }
-
- Main( Locations Q ) {
-
- setLocation(100, 100);
- setSize(640, 480);
- setTitle("Eight Queens Problem (DFS using Backtracing)");
- Board canvas = new Board ( Q );
- button = new MyButton( );
-
-
- Main.go(true);
- Main.window.repaint( );
- }
- };
- button.addActionListener(buttonclk);
-
-
-
- setVisible(true);
- }
- @Override
-
-
-
- }
- /**
- * @param args the command line arguments
- */
-
- int size;
-
-
- //System.out.print("N : ");
- //size = in.nextInt( );
- //System.out.println("Wait .... ");
- size = 8;
- Locations queens = new Locations(size); // stores row of queens
- Main.solution = queens;
-
- public void run() {
- Main.window = new Main(Main.solution);
- }
- });
-
- do {
- int initial = dice.nextInt(size);
- queens.at[(queens.top)++] = initial;
- } while ( solve (queens, window) == false );
-
- public void run() {
- Main.button.setText("D O N E");
- Main.button.removeActionListener(buttonclk);
-
-
- JOptionPane.showMessageDialog(null, "THANK YOU. source code at http://rooparam.blogspot.com", "rooparam", JOptionPane.INFORMATION_MESSAGE);
- }
- };
- Main.button.addActionListener(buttonclk);
- }
- });
-
- for ( int i=0; i< queens.at.length; ++i ) {
-
- }
-
- }
-
- if(queens.top >= queens.at.length)
- return true;
- Main.go(false);
- while (!proceed) {
- try {
-
-
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- try {
-
- public void run() {
- Main.window.repaint();
- }
- });
-
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
-
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- for(int i=0; i < queens.at.length; ++i) {
- if ( isSafe ( queens, i, queens.top ) ) {
- queens.at[queens.top++] = i;
- if( solve ( queens, window) )
- return true;
- }
- }
- queens.top--;
- return false;
- }
- static boolean isSafe(Locations queens, int check, int col) {
- for( int i=0; i < queens.top; ++i ) {
- int col_i = i;
- int row_i = queens.at[i];
- int col_test = col;
- int row_test = check;
- // checking whether in same coloumn of in same row
- if ( col_i == col_test || row_i == row_test )
- return false;
- // digonal check
- if ( ( col_i + row_i ) == ( col_test + row_test ) )
- return false;
- if ( ( col_i - row_i ) == ( col_test - row_test ) )
- return false;
- }
- return true;
- }
- static boolean proceed = false;
-
- static Locations solution = null;
- public static void go(boolean b){
- myLock.lock( );
- try {
- proceed = b;
- } finally
- {
- myLock.unlock( );
- }
- }
- public static final Lock myLock = new ReentrantLock( );
- static MyButton button;
-
- }
Labels:
AI,
algorithms,
computer graphics,
java,
programming,
source
Subscribe to:
Posts (Atom)