from small one page howto to huge articles all in one place
 

search text in:





Poll
Which kernel version do you use?





poll results

Last additions:
using iotop to find disk usage hogs

using iotop to find disk usage hogs

words:

887

views:

186345

userrating:

average rating: 1.7 (102 votes) (1=very good 6=terrible)


May 25th. 2007:
Words

486

Views

250360

why adblockers are bad


Workaround and fixes for the current Core Dump Handling vulnerability affected kernels

Workaround and fixes for the current Core Dump Handling vulnerability affected kernels

words:

161

views:

137535

userrating:

average rating: 1.4 (42 votes) (1=very good 6=terrible)


April, 26th. 2006:

Druckversion . pdf icon
You are here: Programming->C/C++

creating a gauge part 2

Ok, we got our main function and some dummy functions to get some cpu and bandwidth values.

Our graphics will be created using the libgd toolkit, which makes dealing with the images quite easy.

To be able to use it, include gd.h into your program.
#include <gd.h>

MakeImage

void MakeImage(int cpu, int bandwidth)
{
  FILE *fh=fopen("input.png","r");
  FILE *out=fopen("output.jpg","w");
  if (fh & out)
  {
    im=gdImageCreateFromPng(fh);
    fclose(fh);
    drawpointer(im,cpu,1);
    drawpointer(im,bandwidth,2);
    gdImageJpeg(im, out,-1);
    fclose(out);
    gdImageDestroy(im);
  }
}
This creates an image from a png file (input.png), draws 2 pointers into the image and writes a jpg file.
The actual drawing will be done in drawpointer.

drawpointer

We have the cpu and bandwith values stored as an integer from 0-100.
These values need to be converted into angle for our pointer.
angle=(58.0+(246.0*percent)/100.0);
angle=(angle/180.0)*3.1415;  
The first line converts the percent value int an angle between 58 and 246. All functions that will follow need the angle not in °, they need it in a 0-2*pi range.
The second line does the converting.
Optimizing the above lines is left for the reader.
The pointer will be drawn as a solid bright red polygon surrounded by a darker red line.

To get the coordinates for the polygon, the following lines are needed:
points[0].x=-4.0*cos(angle)+XOffset;
points[0].y=-4.0*sin(angle)+YOffset;
points[1].x=4.0*cos(angle)+XOffset;
points[1].y=4.0*sin(angle)+YOffset;
points[2].x=-sin(angle)*radius+2.0*cos(angle)+XOffset;
points[2].y=cos(angle)*radius+2.0*sin(angle)+YOffset;
points[3].x=-sin(angle)*radius-2.0*cos(angle)+XOffset;
points[3].y=cos(angle)*radius-2.0*sin(angle)+YOffset;
gdImageFilledPolygon(im, points, 4, red_bright);
gdImagePolygon(im, points, 4, red_dark);
This will create the needed coordinates and will draw them to the image. XOffset and YOffset will be calculated depending on the given parameters (1 or 2 as the third argument).
The complete function drawpointer:
void drawpointer(gdImagePtr im, int percent, int factor)
{
  double angle=0;
  double radius=55.0;
  gdPoint points[4];
  double XOffset=66.0+132*(factor-1);
  double YOffset=61.0;
  int red_bright;
  int red_dark;
  int white;
  angle=(58.0+(246.0*percent)/100.0);
  angle=(angle/180.0)*3.1415;
  red_bright = gdImageColorAllocate(im, 250, 50, 0);
  red_dark = gdImageColorAllocate(im, 150, 20, 0);
  white = gdImageColorAllocate(im, 255, 255, 255);
  points[0].x=-4.0*cos(angle)+XOffset;
  points[0].y=-4.0*sin(angle)+YOffset;
  points[1].x=4.0*cos(angle)+XOffset;
  points[1].y=4.0*sin(angle)+YOffset;
  points[2].x=-sin(angle)*radius+2.0*cos(angle)+XOffset;
  points[2].y=cos(angle)*radius+2.0*sin(angle)+YOffset;
  points[3].x=-sin(angle)*radius-2.0*cos(angle)+XOffset;
  points[3].y=cos(angle)*radius-2.0*sin(angle)+YOffset;
  gdImageFilledPolygon(im, points, 4, red_bright);
  gdImagePolygon(im, points, 4, red_dark);
}
The last remaining step is to get the actual cpu and bandwidth information. This will be the last part of this tutorial.
rate this article:
current rating: average rating: 1.3 (26 votes) (1=very good 6=terrible)
Your rating:
Very good (1) Good (2) ok (3) average (4) bad (5) terrible (6)

back





Support us on Content Nation
rdf newsfeed | rss newsfeed | Atom newsfeed
- Powered by LeopardCMS - Running on Gentoo -
Copyright 2004-2020 Sascha Nitsch Unternehmensberatung GmbH
Valid XHTML1.1 : Valid CSS : buttonmaker
- Level Triple-A Conformance to Web Content Accessibility Guidelines 1.0 -
- Copyright and legal notices -
Time to create this page: 65.0 ms