Hello everyone, tim here.

I am trying to use/modify a program to model a material in a 2D space. The shape is not too complicated. Now my thought is, instead of drawing it using the functions(which give me a headache, considering the shape has to be split to many piece for drawing), I would draw it using a graphic processing program(photoshop etc) or just import the image from a digital camera. The drawing process should be simple like filling the pixel according to data, cause the material is the same.

Now the problem is, I dont know how to read a image data(I think I should convert it into something like binary?). If I can convert the image data to a binary file, how to process it next? Using ifstream can read the data to memory, it seems(I have not used it before). And then how can I operate the data?

I am using vs 2008 compiling the c++ program. And its just a console program(not using winforms, this program is made by a graduated student). I think it contains some GNU functions in drawing.

I appreciate any help and advice.
Thank you in advance.

5 Years
Discussion Span
Last Post by tim8889

First you have to think if your idea is valid? Even though you are modeling the shape in 2D space, the object to be modeled could still be 3D( is it? ). If it is, then you might be better off trying to model it mathematically, rather than importing it as a picture then applying clever transformation on the image.
What exactly does the shape look like? And as for you initial question, use a external library to import the picture. Then you can use your graphics library or some library to put the image to the screen.


Thank you for the reply.

I only need to model a 2D shape, and as I said it is not too complicated.

The shape can be drawn with only 1 line, actually. it consists of a couple of arc and line.

Now I am not just putting the image to screen, but also need to render the material as the image indicate. Not sure how I should achieve it with draw libraries. Why is my thought invalid though?


>>but also need to render the material as the image indicate.
Explain to me what that means. I can speculate but don't know for sure.

>>Why is my thought invalid though?
Initially, you said 2d space which means that a 3D object can be modeled in 2D space.

Now that I know you need to model 2D shape, you can go with your idea. What graphics library are you using to render the image?


Render the material using following algorithm:

void Material::DrawMaterial(int x0, int y0, int x1, int y1,
							int slicePos, Dimn::Axis sliceDimension,
							MaterialType type)
    int dx_ = x1 - x0;
    int dy_ = y1 - y0;
	int x = 0, y = 0;

	x = x0;
	y = y0;
	if (0 <= x && x < dimx && 0 <= y && y < dimy)
		setMat(x, y, slicePos, type, sliceDimension);
    if (abs(dx_) > abs(dy_)) {          // slope < 1
        float m = (float) dy_ / (float) dx_;      // compute slope
        float b = y0 - m*x0;
        dx_ = (dx_ < 0) ? -1 : 1;
        while (x0 != x1) {
            x0 += dx_;
            x = x0;
			y = (int)round(m*x0+b);
			if (0 <= x && x < dimx && 0 <= y && y < dimy)
				setMat(x, y, slicePos, type, sliceDimension);
    } else if (dy_ != 0) {                              // slope >= 1
        float m = (float) dx_ / (float) dy_;      // compute slope
        float b = x0 - m*y0;
        dy_ = (dy_ < 0) ? -1 : 1;
        while (y0 != y1) {
            y0 += dy_;
            x = (int)round(m*y0+b);
			y = y0;
			if (0 <= x && x < dimx && 0 <= y && y < dimy)
				setMat(x, y, slicePos, type, sliceDimension);

set the material:

void Material::setMat(int x, int y, int z, MaterialType type, Dimn::Axis sliceDimension)
	Dimn::coordinateSwapping(x, y, z, sliceDimension);
	hostmat[x+y*hostpitch+z*hostslice] = type;
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.