Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there, recently I have been trying to experiment with vector graphics in C++. I want to make a Win32 app that reads an svg file and shows it on the screen. The program currently is simple. A window opens using the Win32 API and a .svg file is selected. This svg file's data is then converted to a struct data of my own. The conversion is good. The code of struct is as follows:

#pragma once

#include <vector>
#include "Rect.h"

class VectorTexture
	enum ShapeType
		// Circle: x, y, r
		// Line: x1, y1, x2, y2, width
		// Rectangle: x, y, width, height
		// Square: x, y, a
		// Polygon: x[n], y[n]

	struct Shape
		ShapeType type;
		float data[];

	Rect rect;
	std::vector<Shape> shapes;

Now, the next step is show the svg image. How can I rasterize the vector data that I have, to give me HBITMAP that I can show on the screen? If you know any resources, please put up a link.

Thanks in advance.

What I have tried:

I found a little about DDA and Bresenham algorithms but the only issue is that they are line-drawing algorithms and I am drawing shapes.
Updated 15-Nov-21 5:12am

You may want to look at Getting Started with Direct2D - Win32 apps | Microsoft Docs[^] Microsoft's latest graphics functions. Alternatively there are a number of third party libraries for handling SVG data, and Google will find them for you.
Share this answer
The basic idea behind Bresenham and DDA can be applied to any polynomial function, not just lines (which are polynomials of first degree). The trick is to find a polynomial representation for a nonlinear curve.

Another important thing to consider when looking at bresenham ist that this algorithm represents a line in the form y=f(x), where f(x) is a simple polynomial of the form a*x+b. While there is no reason to do so in case of lines, a better approach is often the representation of a curve as a parametric function of both x and y: {x,y}=f(t). E. g. in case of a line, this function could be f(t)={t,a*t+b}.

In case of a circle with center {0,0} and radius r, you can define a polynomial like this: x*x+y*y-r*r=0. You can use DDA on this expression and find out whether increasing or decreasing x or y leads to a change of sign of this expression - if so, you've found coordinates on the raster image of the curve.

Of course, these kind of things have been implemented on graphic chips 30+ years ago ;-) , so unless you're producing (and hard-wiring) GPUs, there's no reason to do such things. Just use standard graphics functions such as those Richard pointed to in his solution.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900