Last active
October 12, 2015 00:28
-
-
Save justcoding121/3943762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* Tested in NetBeans IDE 6.5.1 with JDK 1.6.0_17 | |
* Generates the configuration space image and saves in png format. | |
* Generates the configuration space matrix and saves in txt format | |
* Please replace FNAME_input and FNAME_output variables as per preffered location of input and output files. | |
*/ | |
import java.io.*; | |
import java.util.*; | |
import java.awt.geom.*; | |
import java.awt.*; | |
import java.awt.image.*; | |
public class configuration_space { | |
final static String FNAME_input = "1.txt"; | |
final static String FNAME_output = "output"; //please don't add .txt extension | |
public Scanner in; | |
public PrintWriter out; | |
void open() throws IOException { | |
in = new Scanner( new File( FNAME_input) ) ; | |
out = new PrintWriter( new File( FNAME_output + ".txt" ) ); | |
} | |
void close() throws IOException { | |
out.close(); | |
} | |
boolean[][] execute() throws IOException { | |
//intializing variables and reading data | |
int no_of_obstacles = in.nextInt(); | |
int no_of_vertices=in.nextInt(); | |
Point2D.Double[] vertices = new Point2D.Double[no_of_vertices]; | |
String obstacle_index_list=new String(); | |
obstacle_index_list=" "; | |
int[] size_of_obstacle= new int[no_of_obstacles]; | |
Point2D.Double base= new Point2D.Double(); | |
Point2D.Double end1= new Point2D.Double(); | |
Point2D.Double end2= new Point2D.Double(); | |
Line2D.Double arm1= new Line2D.Double(); | |
Line2D.Double arm2= new Line2D.Double(); | |
boolean[][] result_matrix=new boolean[361][361]; | |
for(int i=0;i<no_of_vertices;i++) | |
{ | |
vertices[i]= new Point2D.Double(in.nextDouble(),in.nextDouble()); | |
} | |
in.nextLine(); | |
for(int j=0;j<no_of_obstacles;j++) | |
{ | |
String temp=in.nextLine().trim(); | |
size_of_obstacle[j]=temp.split(" ").length; | |
obstacle_index_list=obstacle_index_list+" "+temp; | |
} | |
obstacle_index_list=obstacle_index_list.trim(); | |
base.setLocation(in.nextDouble(), in.nextDouble()); | |
double arm1_length=in.nextDouble(); | |
double arm2_length=in.nextDouble(); | |
//end of reading data from input file | |
//Initializing result matrix to zero | |
for(int n=0;n<361;n++) | |
for(int o=0;o<361;o++) | |
result_matrix[n][o] = false; | |
//Starting to draw obstacle lines and computing intersections | |
int p=0,q=0; | |
for(double theta1=0;theta1<=2*Math.PI;theta1=theta1+Math.PI/180) | |
{ | |
q=0; | |
for(double theta2=0;theta2<=2*Math.PI;theta2=theta2+Math.PI/180) | |
{ | |
boolean ans=false; | |
end1.setLocation(base.x+arm1_length*Math.cos(theta1), base.y+arm1_length*Math.sin(theta1)); | |
end2.setLocation(end1.x+arm2_length*Math.cos(theta2), end1.y+arm2_length*Math.sin(theta2)); | |
arm1.setLine(base, end1); | |
arm2.setLine(end1,end2); | |
String[] obstacle_index= obstacle_index_list.split(" "); | |
int offset=0; | |
for(int k=0;k<no_of_obstacles;k++) | |
{ | |
for(int l=offset;l<size_of_obstacle[k]+offset;l++) | |
for(int m=l+1;m<size_of_obstacle[k]+offset;m++) | |
{ | |
Line2D.Double current_line=new Line2D.Double(vertices[Integer.parseInt(obstacle_index[l])-1],vertices[Integer.parseInt(obstacle_index[m])-1]); | |
if(current_line.intersectsLine(arm1)||current_line.intersectsLine(arm2)) | |
{ | |
ans=ans||true; | |
} | |
} | |
offset=offset+size_of_obstacle[k]; | |
} | |
if(ans) | |
result_matrix[p][q]=true; | |
q++; | |
}p++; | |
} | |
return result_matrix; | |
} | |
//write the result | |
void write(boolean result[][]) throws IOException { | |
int width = 361; | |
int height = 361; | |
int[] pixels = new int [width*height]; | |
for (int j = 0; j < height; j++) { | |
for (int i = 0; i < width; i++) { | |
if (result[i][j] == true) { | |
pixels[j*width + i] = Color.RED.getRGB(); | |
} | |
else { | |
pixels[j*width + i] = Color.WHITE.getRGB(); | |
} | |
} | |
} | |
BufferedImage pixelImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
pixelImage.setRGB(0, 0, width, height, pixels, 0, width); | |
try { | |
File outputfile = new File(FNAME_output+".png"); | |
javax.imageio.ImageIO.write(pixelImage, "png", outputfile); | |
}catch(IOException e){} | |
for(int x=0;x<361;x++) | |
{ | |
for( int y=0;y<361;y++) | |
{ | |
if(result[x][y]==true) | |
out.print("1 "); | |
else out.print("0 "); | |
} | |
out.println(); | |
} | |
out.close(); | |
} | |
public static void main( String[] args ) throws IOException { | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
configuration_space solution = new configuration_space(); | |
solution.open(); | |
solution.write(solution.execute()); | |
solution.close(); | |
} catch ( Exception e ) { | |
throw new RuntimeException( e ); | |
} | |
} | |
}.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment