コード例 #1
0
ファイル: boids.c プロジェクト: Stray/CBofN
void draw_boid(int which, int color)
{
  double x1, x2, x3, y1, y2, y3, a, t;

  /* Plot a line in the direction that it is heading. */
  x3 = xv[which]; y3 = yv[which];
  norm(&x3, &y3);
  x1 = xp[which]; y1 = yp[which];
  x2 = x1 - x3 * len;
  y2 = y1 - y3 * len;
  plot_line(x1, y1, x2, y2, color);

  /* Plot the head of the boid, with the angle of the arrow head
   * indicating its viewing angle.
   */
  t = (x1 - x2) / len;
  t = (t < -1) ? -1 : (t > 1) ? 1 : t;
  a = acos(t);
  a = (y1 - y2) < 0 ? -a : a;

  /* This is for the right portion of the head. */
  x3 = x1 + cos(a + angle / 2) * len / 3.0;
  y3 = y1 + sin(a + angle / 2) * len / 3.0;
  plot_line(x1, y1, x3, y3, color);

  /* This is for the left portion of the head. */
  x3 = x1 + cos(a - angle / 2) * len / 3.0;
  y3 = y1 + sin(a - angle / 2) * len / 3.0;
  plot_line(x1, y1, x3, y3, color);
}
コード例 #2
0
ファイル: viz.c プロジェクト: BSierakowski/personal_code
void plot_vector(double x, double y, double angle, double magnitude) {
    double x2, y2;
    
    x2 = x + (cos(to_rad(angle)) * magnitude);
    y2 = y + (sin(to_rad(angle)) * magnitude);
    
    plot_line(x, y, x2, y2);
}
コード例 #3
0
ファイル: verify_fuzzy.c プロジェクト: sputnick1124/fuzzyc
int main(void)
{
	srand(clock());
	srand48(clock());
	int num_in = 1;
	int in_mfs[1] = {3};
	int num_out = 1;
	int out_mfs[1] = {3};

	int num_rule = prod_i(in_mfs, num_in);
	int consequents[num_rule * num_out];

	int num_params = 3 * (sum_i(in_mfs,num_in) + sum_i(out_mfs,num_out));
	double params[num_params];

	init_params(params, num_in, in_mfs, num_out, out_mfs);
	consequents[0] = 0;
	consequents[1] = 1;
	consequents[2] = 2;
//	rand_params(params, num_in, in_mfs, num_out, out_mfs);
//	rand_consequents(num_out, num_rule, consequents, out_mfs);

	struct Specs * spcs = specs_set(num_in, in_mfs, num_out, out_mfs);
	struct Individual * ind = individual_create(num_params, params, num_rule, num_out, consequents);

	struct Fis * fis = individual_to_fis(ind, spcs);

//	double x[2] = {0.2, 0.25};
	double x[1] = {0.3};
	double out[1];

	evalfis(out,x,fis);
	printf("%f\n",out[0]);
	printf("fitness = %f\n",fit_line(fis));
	plot_line(fis);
	while (1) {
//		scanf("%lf %lf", &x[0], &x[1]);
//		printf("x = [%f, %f]\n",x[0],x[1]);
		if (scanf("%lf", &x[0]) == 0){
			printf("x = [%f]\n", x[0]);
		}
		evalfis(out,x,fis);
		printf("%f\n",out[0]);
	}


	fis_destroy(fis);
	specs_clear(spcs);
	individual_destroy(ind);

	return 0;
}
コード例 #4
0
ファイル: graphics.cpp プロジェクト: Chrismarsh/libmaw
double graphics::plot_line(const d_vec x, const d_vec y,std::string options/*=""*/ )
{
	m_engine->put_double_vector("x",x);
	m_engine->put_double_vector("y",y);

	std::string x_name = "x";
	std::string y_name = "y";

	double handle = plot_line(x_name,y_name,options);

	m_engine->evaluate("clear x y plot_handle");

	return handle;

}
コード例 #5
0
ファイル: canvas.cpp プロジェクト: chiqomar/GLassmt3
/* plot_triangle
 *
 * Use the above functions to plot a whole triangle. Don't forget to
 * take into account the polygon mode. You should be able to render the
 * triangle as 3 points, 3 lines, or a filled in triangle. (v1, v2, v3)
 * are given in window coordinates.
 */
void canvashdl::plot_triangle(vec3f v1, vector<float> v1_varying, vec3f v2, vector<float> v2_varying, vec3f v3, vector<float> v3_varying)
{
	if (polygon_mode == point)
	{
		plot_point(v1, v1_varying);
		plot_point(v2, v2_varying);
		plot_point(v3, v3_varying);
	}
	else if (polygon_mode == line)
	{
		plot_line(v1, v1_varying, v2, v2_varying);
		plot_line(v2, v2_varying, v3, v3_varying);
		plot_line(v3, v3_varying, v1, v1_varying);
	}
	else if (polygon_mode == fill)
	{
		plot_line(v1, v1_varying, v2, v2_varying);
		plot_line(v2, v2_varying, v3, v3_varying);
		plot_line(v3, v3_varying, v1, v1_varying);

		if (v1[0] > v2[0])
		{
			swap(v1, v2);
			swap(v1_varying, v2_varying);
		}
		if (v1[0] > v3[0])
		{
			swap(v1, v3);
			swap(v1_varying, v3_varying);
		}
		if (v2[0] > v3[0])
		{
			swap(v2, v3);
			swap(v2_varying, v3_varying);
		}

		vector<float> ave_varying(v1_varying.size());

		if (shade_model == flat)
			for (int i = 0; i < (int)v1_varying.size(); i++)
				ave_varying[i] = (v1_varying[i] + v2_varying[i] + v3_varying[i])/3.0f;

		plot_half_triangle((vec3i)v1, v1_varying, (vec3i)v2, v2_varying, (vec3i)v3, v3_varying, ave_varying);
		plot_half_triangle((vec3i)v3, v3_varying, (vec3i)v1, v1_varying, (vec3i)v2, v2_varying, ave_varying);
	}
}
コード例 #6
0
ファイル: canvas.cpp プロジェクト: chiqomar/GLassmt3
/* Draw a set of 3D lines on the canvas. Each point in geometry
 * is formatted (vx, vy, vz, nx, ny, nz, s, t). Don't forget to clip
 * the lines against the clipping planes of the projection. You can't
 * just not render them because you'll get some weird popping at the
 * edge of the view.
 */
void canvashdl::draw_lines(const vector<vec8f> &geometry, const vector<int> &indices)
{
	update_normal_matrix();
	mat4f transform = matrices[projection_matrix]*matrices[modelview_matrix];
	vec4f planes[6];
	for (int i = 0; i < 6; i++)
		planes[i] = transform.row(3) + (float)pow(-1.0, i)*(vec4f)transform.row(i/2);

	vector<pair<vec3f, vector<float> > > processed_geometry;
	vector<int> processed_indices;
	processed_geometry.reserve(geometry.size());
	processed_indices.reserve(indices.size());
	vector<int> index_map;
	index_map.resize(geometry.size(), -1);

	for (int i = 0; i < indices.size(); i += 2)
	{
		pair<vec8f, int> x0 = pair<vec8f, int>(geometry[indices[i]], indices[i]);
		pair<vec8f, int> x1 = pair<vec8f, int>(geometry[indices[i+1]], indices[i+1]);
		bool keep = true;

		for (int j = 0; j < 6 && keep; j++)
		{
			float d0 = dot(homogenize(x0.first), planes[j]);
			float d1 = dot(homogenize(x1.first), planes[j]);
			float del = dot(homogenize(x1.first) - homogenize(x0.first), planes[j]);
			float p = -d0/del;

			if (d0 > 0.0 && d1 <= 0.0)
			{
				x1.first = (1-p)*x0.first + p*x1.first;
				x1.second = -1;
			}
			else if (d0 <= 0.0 && d1 > 0.0)
			{
				x0.first = (1-p)*x0.first + p*x1.first;
				x0.second = -1;
			}
			else if (d0 <= 0.0 && d1 <= 0.0)
				keep = false;
		}

		if (keep)
		{
			vector<float> varying;
			vec3f position;
			if (x0.second == -1)
			{
				x0.second = processed_geometry.size();
				position = matrices[viewport_matrix]*homogenize(shade_vertex(x0.first, varying));
				processed_geometry.push_back(pair<vec3f, vector<float> >(position, varying));
			}
			else if (index_map[x0.second] == -1)
			{
				index_map[x0.second] = processed_geometry.size();
				x0.second = processed_geometry.size();
				position = matrices[viewport_matrix]*homogenize(shade_vertex(x0.first, varying));
				processed_geometry.push_back(pair<vec3f, vector<float> >(position, varying));
			}
			else
				x0.second = index_map[x0.second];

			if (x1.second == -1)
			{
				x1.second = processed_geometry.size();
				position = matrices[viewport_matrix]*homogenize(shade_vertex(x1.first, varying));
				processed_geometry.push_back(pair<vec3f, vector<float> >(position, varying));
			}
			else if (index_map[x1.second] == -1)
			{
				index_map[x1.second] = processed_geometry.size();
				x1.second = processed_geometry.size();
				position = matrices[viewport_matrix]*homogenize(shade_vertex(x1.first, varying));
				processed_geometry.push_back(pair<vec3f, vector<float> >(position, varying));
			}
			else
				x1.second = index_map[x1.second];

			processed_indices.push_back(x0.second);
			processed_indices.push_back(x1.second);
		}
	}

	for (int i = 1; i < processed_indices.size(); i+=2)
		plot_line(processed_geometry[processed_indices[i-1]].first, processed_geometry[processed_indices[i-1]].second,
				  processed_geometry[processed_indices[i]].first, processed_geometry[processed_indices[i]].second);
}
コード例 #7
0
 void line(segment s, const unsigned char color[3])
 {
     this->lines.push_back(plot_line(s.a, s.b, color));
 }
コード例 #8
0
ファイル: browser.c プロジェクト: pcwalton/NetSurf
/*
	area: the browser canvas
*/
void browser_redraw_caret( struct gui_window * gw, LGRECT * area )
{
	// TODO: only redraw caret when window is topped.
	if( gw->browser->caret.redraw && gw->browser->caret.requested.g_w > 0 ){
		LGRECT caret;
		struct s_browser * b = gw->browser;
		struct rect old_clip;
		struct rect clip;

		if( b->caret.current.g_w > 0 && b->caret.background.fd_addr != NULL ){
			browser_restore_caret_background( gw, area );
		}

		caret = b->caret.requested;
		caret.g_x -= b->scroll.current.x - area->g_x;
		caret.g_y -= b->scroll.current.y - area->g_y;

		if( !rc_lintersect( area, &caret ) ) {
			return;
		}

		MFDB screen;
		short pxy[8];

		/* save background: */
		//assert( b->caret.background.fd_addr == NULL );
		init_mfdb( app.nplanes, caret.g_w, caret.g_h, 0,
					&b->caret.background );
		init_mfdb( 0, caret.g_w, caret.g_h, 0, &screen );
		pxy[0] = caret.g_x;
		pxy[1] = caret.g_y;
		pxy[2] = caret.g_x + caret.g_w - 1;
		pxy[3] = caret.g_y + caret.g_h - 1;
		pxy[4] = 0;
		pxy[5] = 0;
		pxy[6] = caret.g_w - 1;
		pxy[7] = caret.g_h - 1;
		/* hide the mouse */
		v_hide_c ( app.graf.handle);
		/* copy screen image */
		vro_cpyfm ( app.graf.handle, S_ONLY, pxy, &screen, &b->caret.background);
		/* draw caret: */
		caret.g_x -= area->g_x;
		caret.g_y -= area->g_y;
		clip.x0 = caret.g_x;
		clip.y0 = caret.g_y;
		clip.x1 = caret.g_x + caret.g_w-1;
		clip.y1 = caret.g_y + caret.g_h-1;
		/* store old clip before adjusting it: */
		plot_get_clip( &old_clip );
		/* clip to cursor: */
		plot_clip( &clip );
		plot_line( caret.g_x, caret.g_y, caret.g_x, caret.g_y + caret.g_h,
					plot_style_caret );
		/* restore old clip area: */
		plot_clip( &old_clip );
		/* restore the mouse */
		v_show_c ( app.graf.handle, 1);
		b->caret.current.g_x = caret.g_x + gw->browser->scroll.current.x;
		b->caret.current.g_y = caret.g_y + gw->browser->scroll.current.y;
		b->caret.current.g_w = caret.g_w;
		b->caret.current.g_h = caret.g_h;
	}
}
コード例 #9
0
ファイル: main.cpp プロジェクト: evilmucedin/voronoi
int main(int argc, const char** argv)
{
	// Number of sites to generate
	int count = 200;
	if( argc > 1 )
		count = atol(argv[1]);

	// Image dimension
	int dimension = 512;
	if( argc > 2 )
		dimension = atol(argv[2]);

	voronoi::Point* points = new voronoi::Point[count];
	if( !points )
		return 1;

	int pointoffset = 10; // move the points inwards, for aestetic reasons

	srand(0);

	for( int i = 0; i < count; ++i )
	{
		points[i].x = float(pointoffset + rand() % (dimension-2*pointoffset));
		points[i].y = float(pointoffset + rand() % (dimension-2*pointoffset));
	}

	voronoi::Voronoi generator;
	generator.generate(count, points, dimension, dimension);

	size_t imagesize = dimension*dimension*3;
	unsigned char* image = (unsigned char*)malloc(imagesize);
	memset(image, 0, imagesize);

	unsigned char color[] = {127, 127, 255};
	unsigned char color_pt[] = {255, 255, 255};

	const struct voronoi::Edge* edge = generator.get_edges();
	while( edge )
	{
		plot_line(edge->pos[0].x, edge->pos[0].y, edge->pos[1].x, edge->pos[1].y, image, dimension, 3, color);
		edge = edge->next;
	}

	// Plot the sites
	for( int i = 0; i < count; ++i )
	{
		int index = points[i].y * dimension * 3 + points[i].x * 3;
		image[index+0] = 255;
		image[index+1] = 255;
		image[index+2] = 255;
	}

	delete[] points;

	char path[512];
	sprintf(path, "example.png");
	stbi_write_png(path, dimension, dimension, 3, image, dimension*3);
	printf("wrote %s\n", path);

	free(image);

	return 0;
}
コード例 #10
0
ファイル: viz.c プロジェクト: BSierakowski/personal_code
void viz_update(void) {
    SDL_Rect rect;
    object_list *l;
    object *o;
    int i;
    object *last_object;
    object *last_point;
    
    
    // clear screen
    set_color(1, 1, 1);
    SDL_FillRect(screen, NULL, current_color);
    
    
    
    
    // draw known objects
    for (l = static_objects; l; l = l->next) {
        o = l->data;
        switch (o->kind) {
            case 'b':
                set_color(.4, .4, .4);
                break;
            case 'c':
                set_color(.7, .4, .3);
                break;
            case 'h':
                set_color(0, 1, 0);
                break;
            default:
                set_color(1, 0, 1);
                break;
        }
        plot_point(o->x, o->y, o->a);

    }
    for (l = martians; l; l = l->next) {
        set_color(1, 0, 0);
        o = l->data;
        plot_point(o->x, o->y, 3);
        plot_vector(o->x, o->y, o->a, o->b);
    }
    
    last_point = NULL;
    last_object = NULL;
    for (l = viz_checked_points; l; l = l->next) {
        o = l->data;
        
        if (last_object == NULL) {
            last_object = o;
        }
        
        if (last_point == NULL) {
            last_point = o;
            i = 0;
        }
        
        if (last_object->kind != viz_decided_path) {
            set_color(.5, .5, 1);
        } else {
            set_color(0, 0, 1);
        }
        
        if (last_object->kind != o->kind) {
            plot_line(last_point->x, last_point->y, last_object->x, last_object->y);
            last_point = NULL;
            i = 0;
        }
        
        if (i > 5) {
            plot_line(last_point->x, last_point->y, o->x, o->y);
            last_point = o;
            i = 0;
        }
        
        last_object = o;
        
        i++;
    }
    if (last_point && last_object) {
        plot_line(last_point->x, last_point->y, last_object->x, last_object->y);
    }
    
    // draw destination
    //set_color(.5, .5, 1);
    //plot_point(destination_x, destination_y, 0);
    
    // draw rover
    set_color(0, .5, 0);
    plot_point(last_telemetry.vehicle_x, last_telemetry.vehicle_y, 3);
    
    // draw rover's vector
    plot_vector(last_telemetry.vehicle_x, last_telemetry.vehicle_y, last_telemetry.vehicle_dir, last_telemetry.vehicle_speed);
    
    SDL_Flip(screen);
    
    //fprintf(stderr, "Done\n");
}
コード例 #11
0
ファイル: lorenz.c プロジェクト: Stray/CBofN
int main(int argc, char **argv)
{
  extern int plot_mag;
  extern int plot_inverse;
  int i, j, k;

  /* SSZ is the size of the buffers, while SI is the current index. */
  int ssz, si;

  /* Variables used to calculate the time evolution of the system. */
  double x, y, z;

  /* Buffers to store delay values of all three variables. */
  double *buffer[3];

  /* Pointers into the buffers that always refer to the current,
   * and once delayed values. 
   */
  double *delays[3][2];

  /* Pointers to the pointers that refer what is to be plotted. */
  double **ppx, **ppy;

  /* Variables used to compute auto-scaling. */
  double xmin, xmax, ymin, ymax;

  /* Values that are to be plotted. */
  double px, py, pxx = 0, pyy = 0, temp;

  get_options(argc, argv, options, help_string);

  /* Set up the pointers so that they refer to the proper values to
   * plot with respect to. */
  assign_pointer(&ppx, delays, xp);
  assign_pointer(&ppy, delays, yp);

  if(!data) {
    plot_mag = mag;
    plot_inverse = invert;
    plot_init(width, height, 2, term);
    plot_set_all(0);
  }
  
  /* Initialize the buffer space. */
  ssz = delta + 1;
  for(i = 0; i < 3; i++)
    buffer[i] = xmalloc(sizeof(double) * ssz);
  si = 0;

  /* Initialize the system. */
  x = xx0; y = yy0; z = zz0;

  /* Set insane minimum and maximum guesses. */
  xmin = ymin = 10e10;
  xmax = ymax = -10e10;

  /* For all points (plus the skip and delay values. */
  for(i = 0; i < points + skip + ssz; i++) {

    /* Compute the time evolution of the system. */
    euler(dt, &x, &y, &z);

    /* Save the state so that we can remember delayed values. */
    buffer[0][si] = x; buffer[1][si] = y; buffer[2][si] = z;

    /* For each state value ... , and for each delay value ... */
    for(j = 0; j < 3; j++)
      for(k = 0; k < 2; k++)
        /* Adjust the pointers to refer to the proper delays. */
        delays[j][k] = &buffer[j][(si + ssz - k * delta) % ssz];

    si = (si + 1) % ssz;

    if(data) {
      if(i >= skip + ssz)
        printf("%f\t%f\t%f\n", x, y, z);
    }
    else {
      /* Get the point that we want to plot. */
      px = **ppx; py = **ppy;

      /* If we are still skipping points, adjust the best guesses for
       * the minimum and maximums.
       */
      if(i <= skip + ssz) {
        xmin = (px < xmin) ? px : xmin; xmax = (px > xmax) ? px : xmax;
        ymin = (py < ymin) ? py : ymin; ymax = (py > ymax) ? py : ymax;
      }
      
      /* If this is the last point to be skipped, reset the plotting
       * range based on the minimum and maximums.
       */
      if(i == skip + ssz) {
        temp = (xmax - xmin) * factor; xmin -= temp; xmax += temp;
        temp = (ymax - ymin) * factor; ymin -= temp; ymax += temp;
        plot_set_range(xmin, xmax, ymin, ymax);
      }

      /* Plot a line from the last point to the current point. */
      if(i >= skip + ssz)
        plot_line(pxx, pyy, px, py, 1);

      /* Save the last point. */
      pxx = px; pyy = py;
    }
  }

  if(!data) plot_finish();
  exit(0);
}