Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- typedef vector<int> vi;
- typedef vector<vi> vii;
- struct vec {
- int x, y;
- };
- struct vecf {
- double x, y;
- };
- struct Graph {
- int vertices, edges;
- vector <vec> pos;
- vector <vecf> disp;
- vi grau, adj, loc;
- } g;
- /*
- * GRAPH STUFF
- */
- //gets a graph in the .gra format
- void get_graph(){
- int vertices, edges;
- cin >> vertices >> edges;
- g.vertices = vertices;
- g.edges = edges;
- g.grau = vi(vertices);
- for(int i = 0; i < vertices; ++i){
- cin >> g.grau[i];
- }
- g.adj = vi(edges*2 + 1);
- for(int i = 0; i < edges*2 + 1; ++i){
- cin >> g.adj[i];
- }
- g.loc = vi(vertices + 1);
- for(int i = 0; i < vertices + 1; ++i){
- cin >> g.loc[i];
- }
- }
- //randomizes the distribution of a graph in a frame of w by l
- void randomize_graph(int w, int l){
- srand(time(NULL));
- for(int i = 0; i < g.vertices; ++i){
- g.pos[i].x = rand() % (l + 1);
- g.pos[i].y = rand() % (w + 1);
- }
- }
- /*
- * ALGORITHM STUFF
- */
- double attraction_force(int d, int k){
- return (d*d)/k;
- }
- double repulsion_force(int d, int k){
- return -(k*k)/d;
- }
- //cooling function
- void cool(double& t){
- t = 0.95*t;
- }
- /*
- // diference between vectors
- bool operator!= (const vec& a, const vec& b){
- return ((a.x != b.x) or (a.y != b.y));
- }
- */
- //gets frame size interations and temperature
- //simulates graph
- int main(){
- int height, width, iterations;
- double k, t;
- cin >> height, width >> iterations;
- t = width/10;
- //force constant
- //C*sqrt(area/#vertices)
- k = sqrt(height*width/g.vertices);
- for(int k = 0; k < iterations; ++k){
- //calculate repulsive forces
- for(int i = 0; i < g.vertices; ++i){
- g.disp[i].x = g.disp[i].y = 0;
- for(int j = 0; j < g.vertices; ++j){
- if(i != j){
- vecf d;
- d.x = g.pos[i].x - g.pos[j].x;
- d.y = g.pos[i].y - g.pos[j].y;
- double modulus = sqrt(d.x*d.x + d.y*d.y);
- double force = repulsion_force(modulus, k);
- double mult = force / modulus;
- g.disp[i].x = g.disp[i].x + d.x*mult;
- g.disp[i].y = g.disp[i].y + d.y*mult;
- }
- }
- }
- //calculate atractive forces
- for(int i = 0; i < g.edges; ++i){
- for(int j = g.loc[i]; j < g.loc[i + 1]; ++j){
- vecf d;
- d.x = g.pos[i].x - g.pos[g.adj[j]].x;
- d.y = g.pos[i].y - g.pos[g.adj[j]].y;
- double modulus = sqrt(d.x*d.x + d.y*d.y);
- double force = attraction_force(modulus, k);
- double mult = force / modulus;
- g.disp[i].x = g.disp[i].x + d.x*mult;
- g.disp[i].y = g.disp[i].y + d.y*mult;
- }
- }
- //limit displacement by temperature t
- //prevent displacement outside of the frame
- for(int i = 0; i < g.vertices; ++i){
- double modulus = sqrt(g.disp[i].x*g.disp[i].x + g.disp[i].y*g.disp[i].y);
- double prod_x = min(g.disp[i].x, t)/modulus;
- double prod_y = min(g.disp[i].y, t)/modulus;
- g.pos[i].x = g.pos[i].x + g.disp[i].x*prod_x;
- g.pos[i].y = g.pos[i].y + g.disp[i].y*prod_y;
- g.pos[i].x = min(width/2 , max(-1*width/2, g.pos[i].x));
- g.pos[i].y = min(height/2, max(-1*height/2, g.pos[i].y));
- }
- cool(t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement