Exemplo n.º 1
0
void DrawPathfinderMap(string mapFile) {
	SetPenColor("White");
	FillBox(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
	SetPenColor("Black");
	if (mapFile != "") {
		MovePen(0, 0);
		DrawNamedPicture(mapFile);
	}
}
Exemplo n.º 2
0
int main() {
	InitGraphics();
	SetWindowTitle("CS106 Pathfinder");
    cout << "This masterful piece of work is a graph extravaganza!" << endl
        << "The main attractions include a lovely visual presentation of the graph" << endl
        << "along with an implementation of Dijkstra's shortest path algorithm and" << endl
        << "the computation of a minimal spanning tree.  Enjoy!" << endl;
	Graph graph;
	Map<coordT> nodeCor;
	string loc1, loc2, image;
	image = loadGraphFile(graph, nodeCor);
	drawMap(graph, nodeCor);
	while (true) {
		int choice = promptForChoice();
		switch (choice) {
			case 1:
				graph.clear();
				nodeCor.clear();
				image = loadGraphFile(graph, nodeCor);
				drawMap(graph, nodeCor);
				break;
			case 2:
				cout << endl;
				loc1 = getLocName("Click on starting location...  ", nodeCor);
				loc2 = getLocName("Click on ending location...  ", nodeCor);
				displayMinPath(graph, nodeCor, loc1, loc2);
				break;
			case 3:
				InitGraphics();
				DrawNamedPicture(image);
				drawVertices(nodeCor);
				displayMST(graph, nodeCor);
				break;
			case 4:
				displayDomSet(graph, nodeCor);
				break;
			case 5: exit(0);
		}
	} 

    return (0);
}
Exemplo n.º 3
0
string ReadGraph(Set<nodeT *> &graph, Map<nodeT *> &graphMap, string datafile) {
    
    ifstream inner;
    inner.open(datafile.c_str());
    if (inner.fail()) Error("Can't open file");
    
    string background;
    inner >> background;
    string name;
    inner >> name;
    
    //cout << "Background file: " << background << endl;
    //cout << name << endl;
    
    while (true) {
        nodeT *n = new nodeT;
        if (inner.fail()) break; // no more lines to read
        //double x, y;
        inner >> n->name;
        if (n->name == "ARCS")
            break;
        else
            inner >> n->x >> n->y;
        
        graphMap.add(n->name,n);
        graph.add(n);
        
        //cout << n->name << " " << n->x << ":" << n->y << endl;
        
    }
    
    //    cout << endl << endl;
    //    cout << "THE GRAPH MAP" << endl;
    //    cout << endl;
    //    PrintGraphMap(graphMap);
    
    while (true) {
        if (inner.fail()) break; // no more lines to read
        string name1, name2;
        double distance;
        inner >> name1 >> name2 >> distance;
        if (name1 == "") break;
        //cout << name1 << " <-> " << name2 << " :" << distance << endl;
        
        arcT *arc1 = new arcT;
        arcT *arc2 = new arcT;
        
        arc1->start = graphMap.get(name1);
        arc1->end = graphMap.get(name2);
        arc1->distance = distance;
        
        arc2->end = graphMap.get(name1);
        arc2->start = graphMap.get(name2);
        arc2->distance = distance;
        
        nodeT *node1 = graphMap.get(name1);
        nodeT *node2 = graphMap.get(name2);
        
        node1->links.add(arc1);
        node2->links.add(arc2);
    }
    
    inner.close();
    
    //    cout << endl << endl;
    //    cout << "THE GRAPH" << endl;
    //    PrintGraphMap(graphMap);
    //    PrintGraph(graph);
    
    MovePen(0,0);
    DrawNamedPicture(background);
    DrawNodesAndArcs(graph);
    
    return background;
}
Exemplo n.º 4
0
int main()
{
    Set<nodeT *> graph;
    Map<nodeT *> graphMap;
    
	InitGraphics();
	SetWindowTitle("Pathfinder");
    
    string backgroundFile;
    bool timeToQuit = false;
    
    
    cout << "This masterful piece of work is a graph extravaganza!" << endl;
    
    while (true) {
        
        cout << "Your options are:" << endl;
        cout << "(1) Choose a new graph data file" << endl;
        cout << "(2) Find shortest path using Dijkstra's algorithm" << endl;
        cout << "(3) Compute the minimal spanning tree using Kruskal's algorithm" << endl;
        cout << "(4) Determine if the graph has cycles" << endl;
        cout << "(5) Quit" << endl;
        cout << "Enter choice: ";
        
        int choice = GetInteger();
        string datafile;
        void UpdateDisplay();
        
        switch (choice) {
            case 1:
                cout << "Enter the name of the data file: ";
                datafile = GetLine();
                //datafile = "Small.txt";
                //datafile = "USA.txt";
                //datafile = "Stanford.txt";
                graph.clear();
                graphMap.clear();
                backgroundFile = ReadGraph(graph, graphMap, datafile);
                break;
            case 2:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    UpdateDisplay();
                    MovePen(0,0);
                    DrawNamedPicture(backgroundFile);
                    DrawNodesAndArcs(graph);
                    cout << "(2) Finding shortest path using Dijkstra's algorithm" << endl;
                    HandleShortestPath(graph);
                }
                break;
            case 3:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    cout << "(3) Computing the minimal spanning tree using Kruskal's algorithm" << endl;
                    UpdateDisplay();
                    MovePen(0,0);
                    DrawNamedPicture(backgroundFile);
                    //DrawNodesAndArcs(graph);
                    minimumSpanningTree(graph, graphMap);
                }
                break;
            case 4:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    graphT graph2;
                    graph2.allNodes = graph;
                    
                    cout << "(4) Determining if the graph has cycles" << endl;
                    if (IsCyclicGraph(graph2))
                        cout << "Yes, it is cyclic" << endl;
                    else
                        cout << "No, it is not cyclic" << endl;
                }
                break;
            case 5:
                cout << "Thanks for using Pathfinder. Bye!" << endl;
                timeToQuit = true;
                break;
                
            default:
                cout << "Invalid choice" << endl;
                break;
        }
        
        if (timeToQuit) break;
        
    }
    return (0);
}
Exemplo n.º 5
0
int main()
{
    InitGraphics();
    SetWindowTitle("CS106 Pathfinder");
    cout << "This masterful piece of work is a graph extravaganza!" << endl
        << "The main attractions include a lovely visual presentation of the graph" << endl
        << "along with an implementation of Dijkstra's shortest path algorithm and" << endl
        << "the computation of a minimal spanning tree.  Enjoy!" << endl;
    //double w=GetPictureWidth("Stanford.jpg");
    //double h=GetPictureHeight("Stanford.jpg");
    //cout<<w<<":"<<h<<endl;
    //cout<<GetWindowWidth()<<":"<<GetWindowHeight()<<endl;

    //define the map data source
    const string Stanford="Stanford.txt";
    const string Small="Small.txt";
    const string USA="USA.txt";
    const string PicStanford="stanford.jpg";
    const string PicUSA="USA.bmp";

    //define the map data struct
    map<string,infoT> point;
    set<arcT> arc;

    while(true){
        cout<<"0) Stanford.txt   1) Small.txt  2) USA.txt\n";
        cout<<"Please select the map data source(0,1 or 2):\n";
        int flag;
        cin>>flag;
        if(flag==0){
            //:wq
            //SetWindowSize(7,5);
            //double w=GetPictureWidth("stanford2.png");
            //double h=GetPictureHeight("stanford2.png");
            //cout<<w<<":"<<h<<endl;
            //SetWindowSize(w,h);
            DrawNamedPicture("stanford2.png");
            readMapData(point,arc,Stanford); 
            break;
        }
        //double w=GetPictureWidth("USA.bmp");
        //double h=GetPictureHeight("USA.bmp");
        //cout<<w<<":"<<h<<endl;
        SetWindowSize(6.3,4);
        if(flag==1){
            DrawNamedPicture("USA.bmp");
            readMapData(point,arc,Small);
            break;
        }
        else if(flag==2){
            DrawNamedPicture("USA.bmp");
            readMapData(point,arc,USA);
            break;
        }
        else{
            cout<<"Wrong number!"<<endl;
        }

    }
    //draw map
    DrawLabeledPointAndLine(point,arc);

    map<string,infoT>::iterator it=point.begin();
    while(it!=point.end()){
        cout<<it->first<<":"<<endl;
        set<laborT> la=it->second.labor;
        set<laborT>::iterator i=la.begin();
        while(i!=la.end()){
            cout<<"     "<<(*i).name<<":"<<(*i).distance<<endl;
            i++;
        }
        it++;
    }

    while(true){
        cout<<"What do you want?"<<endl;
        cout<<"0) draw the optional path between the slected points"<<endl;
        cout<<"1) draw the minimal spanning tree"<<endl;
        cout<<"Please select(0 or 1):";
        int flag;
        cin>>flag;
        if(flag==0){
            //draw the optional path
            DrawOptionalPath(point,arc);
            break;
        }
        else if(flag==1){
            //draw the minimal spanning tree
            DrawMinimalSpanningTree(point,arc);
            break;
        }
        else{
            cout<<"wrong number!"<<endl;
        }
    }
    return (0);
}
Exemplo n.º 6
0
string drawBackGround(ifstream &in) {
	string image;
	in >> image;
	DrawNamedPicture(image);
	return image;
}