コード例 #1
0
void draw_line(double x0, double y0, double x1, double y1)
{
  int i;
  double x,y;
  double dx,dy;
  double xinc,yinc;
  double lenosuh;
  int result;
  int width,height;

  /* set the clipping window */
  osuGetFramebufferSize (&width, &height);
  set_clip_window (0.0f, 0.0f, width - 0.51f, height - 0.51f);

  /* clip the line in 2D */
  result = clip_line (&x0, &y0, &x1, &y1);

  /* return if line is entirely outside the clip window */
  if (result == 0)
    return;

  /* incremental line drawing */

  dx = x1 - x0;
  dy = y1 - y0;

  /* determine whether horizontal or vertical change is larger */

  if (fabs(dx) > fabs(dy))
    lenosuh = (double)fabs(dx);
  else
    lenosuh = (double)fabs(dy);

  /* special case to avoid dividing by zero */

  if (lenosuh == 0) {
    osuWritePixel ((int) floor(x0+0.5), (int) floor(y0+0.5), 255, 255, 255);
    return;
  }

  xinc = dx / lenosuh;
  yinc = dy / lenosuh;

  x = x0;
  y = y0;

  /* write "lenosuh" number of pixels along the line */

  for (i = 0; i <= lenosuh; i++) {
    osuWritePixel ((int) floor(x+0.5), (int) floor(y+0.5), 255, 255, 255);
    x += xinc;
    y += yinc;
  }
}
コード例 #2
0
void draw_line(float x0, float y0, float x1, float y1) {
	GLint viewport[4];
	int i;
	float x, y;
	float dx, dy;
	float xinc, yinc;
	float length = 0.;
	int result;
	int width, height;
	Pixel white = { 1, 1, 1 };

	/* set the clipping window */

	glGetIntegerv(GL_VIEWPORT, viewport);
	width = abs(viewport[2] - viewport[0]);
	height = abs(viewport[3] - viewport[1]);

	set_clip_window(0.0, 0.0, width - 0.51, height - 0.51);

	/* clip the line in 2D */
	result = clip_line(&x0, &y0, &x1, &y1);

	/* return if line is entirely outside the clip window */
	if (result == 0)
		return;

	/* incremental line drawing */

	dx = x1 - x0;
	dy = y1 - y0;

	/* determine whether horizontal or vertical change is larger */

	if (fabs(dx) > fabs(dy))
		length = fabs(dx);
	else
		length = fabs(dy);

	/* special case to avoid dividing by zero */

	if (length == 0) {
		setPixel(x0, y0, white);
		return;
	}

	xinc = dx / length;
	yinc = dy / length;

	x = x0;
	y = y0;

	/* write "length" number of pixels along the line */

	for (i = 0; i <= length; i++) {

		if (x < displayImage.width && y < displayImage.height)
			setPixel(y, x, white);

		x += xinc;
		y += yinc;
	}
	glFlush();
}
コード例 #3
0
ファイル: streamline.cpp プロジェクト: haolly/fluid3dsjtu
void Streamline::streamline_creator(
  VectorField *field,
  float xx,
  float yy,
  float len1,
  float len2,
  float dlen
)
{
  int i;
  float x,y;

  /* integrate to midpoint */

#if 1
  float dl = 0.5 * (len1 - len2);
  int num = abs ((int) (dl / dlen)) + 1;
  float dt = dl / num;
  x = xx;
  y = yy;
  for (i = 0; i < num; i++) {
    x += dt * field->xval(x,y);
    y += dt * field->yval(x,y);
  }
  xx = x;
  yy = y;
  float new_len = 0.5 * (len1 + len2);
  len1 = len2 = new_len;
#endif

  /* origin must be within [0,1] x [0,aspect] */

  if (xx < 0 || xx > 1 || yy < 0 || yy > field->getaspect()) {
    float x = xx;
    float y = yy;
    clamp_to_screen (x, y, field->getaspect());
    xx = x;
    yy = y;
  }

  /* set values */

  vf = field;
  xorig = xx;
  yorig = yy;
  length1 = len1;
  length2 = len2;
  delta = dlen;
  frozen = 0;      /* a "frozen" streamline is one that isn't to be moved */
  label = label_default;
  start_reduction = start_reduction_default;
  end_reduction = end_reduction_default;
  arrow_type = arrow_type_default;
  arrow_length = arrow_length_default;
  arrow_width = arrow_width_default;
  arrow_steps = arrow_steps_default;
  intensity = intensity_default;
  taper_head = taper_head_default;
  taper_tail = taper_tail_default;
  tail_clipped = 0;
  head_clipped = 0;

  /* values at pixels */
  num_values = 0;
  max_values = 10;
  values = new PixelValue[max_values];

  /* determine spacing of sample points */

  int samples1 = (int) (length1 / delta);
  int samples2 = (int) (length2 / delta);
  delta = (length1+length2) / (samples1+samples2);

  samples = samples1 + samples2 + 1;
  pts = new SamplePoint[samples];

  /* calculate sample points along streamline */

  x = xorig;
  y = yorig;

  xs(samples2) = x;
  ys(samples2) = y;

#define MIN_STEP 0.2

  /* step forward */

  int count1 = 0;
  for (i = 0; i < samples1; i++) {

    float slen = vf->integrate (x, y, delta, 0, x, y);
    if (slen < MIN_STEP)
      break;

    xs(samples2 + i + 1) = x;
    ys(samples2 + i + 1) = y;
    count1++;

    /* clip to screen */
    if (x < 0 || x > 1 || y < 0 || y > vf->getaspect()) {
      set_clip_window (0.0, 1.0, 0.0, vf->getaspect());
      float x0 = xs(samples2 + i);
      float y0 = ys(samples2 + i);
// printf ("clip #1 before: %f %f %f %f\n", x0, y0, x, y);
      clip_line (x0, y0, x, y);
// printf ("clip #1  after: %f %f %f %f\n", x0, y0, x, y);
// printf ("\n");
      xs(samples2 + i + 1) = x;
      ys(samples2 + i + 1) = y;
      head_clipped = 1;
      break;  /* we're off the screen, so don't extend streamline further */
    }
  }

  /* step backwards */

  x = xorig;
  y = yorig;

  int count2 = 0;
  for (i = samples2-1; i >= 0; i--) {

    float slen = vf->integrate (x, y, -delta, 0, x, y);
    if (slen < MIN_STEP)
      break;

    xs(i) = x;
    ys(i) = y;
    count2++;

    /* clip to screen */
    if (x < 0 || x > 1 || y < 0 || y > vf->getaspect()) {
      set_clip_window (0.0, 1.0, 0.0, vf->getaspect());
      float x0 = xs(i+1);
      float y0 = ys(i+1);
// printf ("clip #2 before: %f %f %f %f\n", x0, y0, x, y);
      clip_line (x0, y0, x, y);
// printf ("clip #2  after: %f %f %f %f\n", x0, y0, x, y);
// printf ("\n");
      xs(i) = x;
      ys(i) = y;
      tail_clipped = 1;
      break;  /* we're off the screen, so don't extend streamline further */
    }
  }

  /* compute number of samples, taking into account the portions of */
  /* the streamline that may have been clipped */

  samples = count1 + count2 + 1;

  /* may have to shift the sample points */

  int diff2 = samples2 - count2;
  if (diff2 > 0)
    for (i = 0; i < samples; i++) {
      xs(i) = xs(i + diff2);
      ys(i) = ys(i + diff2);
    }

  int smp = samples - 1;
  if (xs(0) < 0 || xs(0) > 1 || ys(0) < 0 || ys(0) > vf->getaspect() ||
    xs(smp) < 0 || xs(smp) > 1 || ys(smp) < 0 || ys(smp) > vf->getaspect()) {
    printf ("origin: %f %f\n", xx, yy);
    printf ("ends:   %f %f %f %f\n", xs(0), ys(0), xs(smp), ys(smp));
    printf ("\n");
  }

  /* compute intensity tapering */

  for (i = 0; i < samples; i++) {
    float t = i / (samples - 1.0);
    if (t < taper_tail) {
      t = t / taper_tail;
      pts[i].intensity = t;
    }
    else if (1 - t < taper_head) {
      t = (1 - t) / taper_head;
      pts[i].intensity = t;
    }
    else
      pts[i].intensity = 1.0;
  }
}