Example #1
0
   void main()
      {          int ch;
	clrscr();
	while(1)
	{
	printf("1.Enter values\n 2.Display values\n");
	printf("3.Enter a value after\n 4.Enter a value before\n");
	printf("5.Delete after\n 6.Delete before\n");
	    scanf("%d",&ch);
	    switch(ch)
	    {
	      case 1:
	      create();
	      break;
	      case 2:
	      disp();
	      break;
	      case 3:
	      insa();
	      break;
	      case 4:
	      insb();
	      break;
	      case 5:
	      dela();
	      break;
	      case 6:
	       delb();
	       break;
	      case 7:
	      return;
	    }
	}
      }
Example #2
0
pair<
	vector<pair<int, Vec2>>,
vector<pair<int, int>>

>
random_graph(int pick_count, float distrib_radius)
{
	vector<pair<int, Vec2>> points;
	vector<pair<int, int>> graph;

	//float distrib_radius;// = cfg.getvar<float>("distrib_radius");
	paused_distr paus_distr(distrib_radius, 5, 321);
	//int winheight = cfg.getvar<Vec2i>("windowsize").y;
	//scaler scl(winheight*0.9f, { 0,0 });

	DelaunayTriangulation dela(1, 1);

	//int pick_count = cfg.getvar<int>("pick_count");
	for (int i = 0; i < pick_count; ++i)
	{
		Vec2 cand;
		int retcode = -1;
		do
		{
			retcode = paus_distr.pick5(cand);
		} while (retcode == 2);
		if (retcode != -1 && retcode != 0)
		{
			dela.AddPoint(Point(cand.x, cand.y));
			//glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f));
			//glob_pts.push_back(mkcircle(scl(cand), Color::White, 3.f));
		}

	}
	//msg(glob_pts.size());
	for (auto&triangle : dela.triangles)
	{
		int
			a = triangle.get()->v[0],
			b = triangle.get()->v[1],
			c = triangle.get()->v[2];
		Point
			A = dela.points[a],
			B = dela.points[b],
			C = dela.points[c];
		if (
			A.x == 0 || A.x == 1
			|| A.y == 0 || A.y == 1
			|| B.x == 0 || B.x == 1
			|| B.y == 0 || B.y == 1
			|| C.x == 0 || C.x == 1
			|| C.y == 0 || C.y == 1
			)
		{
			continue;
		}
		points.push_back({ a,{ float(dela.points[a].x),float(dela.points[a].y) } });
		points.push_back({ b,{ float(dela.points[b].x),float(dela.points[b].y) } });
		points.push_back({ c,{ float(dela.points[c].x),float(dela.points[c].y) } });

		graph.push_back({ a,b });
		graph.push_back({ b,c });
		graph.push_back({ a,c });

		// those are the ones!
		//segment(scl(Vec2(A.x, A.y)), scl(Vec2(B.x, B.y)));
		//segment(scl(Vec2(C.x, C.y)), scl(Vec2(B.x, B.y)));
		//segment(scl(Vec2(A.x, A.y)), scl(Vec2(C.x, C.y)));

		//segment(Vec2(A.x, A.y), Vec2(B.x, B.y));
		//segment(Vec2(C.x, C.y), Vec2(B.x, B.y));
		//segment(Vec2(A.x, A.y), Vec2(C.x, C.y));
		//segment(pts[a], pts[b]);
		//segment(pts[a], pts[c]);
		//segment(pts[c], pts[b]);
	}
	return make_pair(points, graph);
}
Example #3
0
function<void()>
////////////////////////// TYPE APP NAME ON LINE BELOW //////////////////////////
delaun_distr

(RenderWindow&window, ui &UI) {
	return [&window, &UI]() {
#ifndef COMMON_INITS
		//void smoothmouse(RenderWindow&window, ui &UI){

		// tidy code organization, here we predeclare methods just like a header would, it's redundant but necessary
		function<void()> draw, loop, init, update;
		function<void(Vec2 pos)> mousemoved;
		function<void(sf::Mouse::Button button)> mouseclick, mouserelease;
		function<void(Event&e)> treatotherevent;
		function<void(Keyboard::Key k)> treatkeyevent;

		// we don't have a class/interface/struct with data, everything is local to this function, like a classic stack that all programs are anyway.
		Texture cursor_tx;
		Sprite cursor;
		configfile cfg;
		cfg.init("bedlab.cfg");
		Color background = cfg.getvar<Color>("background");
		if (!cursor_tx.loadFromFile(cfg.getstr("cursor")))
			cout << "did not load cursor" << endl;
		cursor.setTexture(cursor_tx);
		cursor.setOrigin(3, 3);
		window.setMouseCursorVisible(false);
		Vec2 mpos;
		bool leftclicked = false, rightclicked = false;

		// view and zoom
		View view, ui_view;
		ui_view = view = window.getDefaultView();
		float zoomlevel = 1;
		Vec2 mpos_abs;
#endif // COMMON_INITS
		// ************************ CODE BEGIN ************************
		
		auto random_graph = [](int pick_count, float distrib_radius)
		{
			vector<pair<int,Vec2>> points;
			vector<pair<int, int>> graph;
			

			//float distrib_radius;// = cfg.getvar<float>("distrib_radius");

			paused_distr paus_distr(distrib_radius, 5, 321);
			//int winheight = cfg.getvar<Vec2i>("windowsize").y;
			//scaler scl(winheight*0.9f, { 0,0 });

			DelaunayTriangulation dela(1, 1);

			//int pick_count = cfg.getvar<int>("pick_count");
			for (int i = 0; i < pick_count; ++i)
			{
				Vec2 cand;
				int retcode = -1;
				do
				{
					retcode = paus_distr.pick5(cand);
				} while (retcode == 2);
				if (retcode != -1 && retcode != 0)
				{
					dela.AddPoint(Point(cand.x, cand.y));
					//glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f));
					//glob_pts.push_back(mkcircle(scl(cand), Color::White, 3.f));
					points.push_back({ points.size(),cand });
				}

			}
			//msg(glob_pts.size());
			for (auto&triangle : dela.triangles)
			{
				int
					a = triangle.get()->v[0],
					b = triangle.get()->v[1],
					c = triangle.get()->v[2];
				Point
					A = dela.points[a],
					B = dela.points[b],
					C = dela.points[c];
				if (
					A.x == 0 || A.x == 1
					|| A.y == 0 || A.y == 1
					|| B.x == 0 || B.x == 1
					|| B.y == 0 || B.y == 1
					|| C.x == 0 || C.x == 1
					|| C.y == 0 || C.y == 1
					)
				{
					continue;
				}

				graph.push_back({a,b});
				graph.push_back({b,c});
				graph.push_back({a,c});

				// those are the ones!
				//segment(scl(Vec2(A.x, A.y)), scl(Vec2(B.x, B.y)));
				//segment(scl(Vec2(C.x, C.y)), scl(Vec2(B.x, B.y)));
				//segment(scl(Vec2(A.x, A.y)), scl(Vec2(C.x, C.y)));

				//segment(Vec2(A.x, A.y), Vec2(B.x, B.y));
				//segment(Vec2(C.x, C.y), Vec2(B.x, B.y));
				//segment(Vec2(A.x, A.y), Vec2(C.x, C.y));
				//segment(pts[a], pts[b]);
				//segment(pts[a], pts[c]);
				//segment(pts[c], pts[b]);
			}
			return make_pair(points, graph);
		};


		float distrib_radius = cfg.getvar<float>("distrib_radius");

		paused_distr paus_distr(distrib_radius, 5, 321);
		int winheight = cfg.getvar<Vec2i>("windowsize").y;
		scaler scl(winheight*0.9f, { 0,0 });

		DelaunayTriangulation dela(1, 1);

		int pick_count = cfg.getvar<int>("pick_count");
		for(int i = 0; i < pick_count; ++i)
		{ 
			Vec2 cand;
			int retcode = -1;
			do
			{
				retcode = paus_distr.pick5(cand);
			} while (retcode == 2);
			if (retcode != -1 && retcode != 0)
			{
				dela.AddPoint(Point(cand.x, cand.y));

				//glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f));
				glob_pts.push_back(mkcircle(scl(cand), Color::White, 3.f));
			}

		}
        msg(glob_pts.size());
		for (auto&triangle : dela.triangles)
		{
			int
				a = triangle.get()->v[0],
				b = triangle.get()->v[1],
				c = triangle.get()->v[2];
			Point
				A = dela.points[a],
				B = dela.points[b],
				C = dela.points[c];
			if (
				A.x == 0 || A.x == 1
				|| A.y == 0 || A.y == 1
				|| B.x == 0 || B.x == 1
				|| B.y == 0 || B.y == 1
				|| C.x == 0 || C.x == 1
				|| C.y == 0 || C.y == 1
				) 
			{
				continue;
			}
			
			segment(scl(Vec2(A.x, A.y)), scl(Vec2(B.x, B.y)));
			segment(scl(Vec2(C.x, C.y)), scl(Vec2(B.x, B.y)));
			segment(scl(Vec2(A.x, A.y)), scl(Vec2(C.x, C.y)));
			//segment(Vec2(A.x, A.y), Vec2(B.x, B.y));
			//segment(Vec2(C.x, C.y), Vec2(B.x, B.y));
			//segment(Vec2(A.x, A.y), Vec2(C.x, C.y));
			//segment(pts[a], pts[b]);
			//segment(pts[a], pts[c]);
			//segment(pts[c], pts[b]);
		}

		barstack bst(FONT, FONTSIZE);
		slider_finder sl;
		size_t edit_mode = 1;

		// ************************ INITS END ************************
		//then we actually define the functions, note how all function captures the local context by reference
#ifndef LOOP_LAMBDAS
		draw = [&]() {
			window.setView(view);
			// object draw, AFTER normal view

			//Vec2 cand;
			//int retcode = -1;
			//do
			//{
			//	retcode = paus_distr.pick4(cand);
			//} while (retcode == 2);
			//if (retcode != -1 && retcode != 0)
			//{
			//	//glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f));
			//	glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f));
			//}


			//////////////// obj draw START ////////////////
			for (auto&a : glob_pts)window.draw(a);
			for (auto&a : glob_rects)window.draw(a);
			for (auto&a : glob_vert)window.draw(a);
			for (auto&a : glob_texts)window.draw(a);
			//window.draw(nearest_pt);

			//////////////// obj draw END ////////////////
			// UI draw, AFTER ui view and BEFORE other draw
			window.setView(ui_view);
			bst.draw(window);
			// nothing after here please
			UI.draw(window);
			window.draw(cursor);
		};
		update = [&]() {

		};
		treatkeyevent = [&](Keyboard::Key k) {
			switch (k)
			{
			case Keyboard::BackSpace:
				glob_pts.clear();
				glob_texts.clear();
				glob_rects.clear();
				glob_vert.clear();
				break;
			case Keyboard::Space:

				break;
			case Keyboard::Q: break;
			}
		};
		mousemoved = [&](Vec2 pos) {
			cursor.setPosition(pos);
			if (leftclicked) sl.mouse_callback(pos);
		};
		mouseclick = [&](sf::Mouse::Button button) {
			if (button == Mouse::Button::Left) leftclicked = true;
			if (button == Mouse::Button::Right) rightclicked = true;
		};
		mouserelease = [&](sf::Mouse::Button button) {
			if (button == Mouse::Button::Left) leftclicked = false;
			if (button == Mouse::Button::Right) rightclicked = false;
			sl.release();
		};
		loop = [&]() {
			while (window.isOpen())
			{
				sf::Event event;
				while (window.pollEvent(event))
				{
					switch (event.type)
					{
					case sf::Event::KeyPressed:
						if (event.key.code == sf::Keyboard::Escape)
							window.close();
						treatkeyevent(event.key.code);
						break;
					case sf::Event::Closed:
						window.close();
						break;
					case sf::Event::MouseButtonPressed:
						mouseclick(event.mouseButton.button);
						break;
					case sf::Event::MouseButtonReleased:
						mouserelease(event.mouseButton.button);
						break;
					case sf::Event::MouseMoved:
						mpos = Vec2(event.mouseMove.x, event.mouseMove.y);
						mousemoved(mpos);
						break;
					default:
						treatotherevent(event);
						break;
					}
				}

				window.clear(background);
				update();
				draw();
				window.display();
			}
		};
		treatotherevent = [&](Event&e) {
			if (e.type == Event::MouseWheelMoved && e.mouseWheel.delta)
			{
				mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view);

				//view = window.getView();
				if (e.mouseWheel.delta < 0)
				{
					zoomlevel *= 2.f;
					view.setSize(view.getSize()*2.f);
					view.setCenter(interp(mpos_abs, view.getCenter(), 2.f));
					//view.setCenter(interp(mpos_abs, view.getCenter(), 2.f));
				}
				if (e.mouseWheel.delta > 0)
				{
					zoomlevel *= 0.5;
					view.setSize(view.getSize()*.5f);
					view.setCenter(.5f*(view.getCenter() + mpos_abs));
					//view.setCenter(.5f*(view.getCenter() + mpos_abs));
				}
				window.setView(view);
			}
		};
		loop();
#endif // LOOP_LAMBDAS
	};
}