Skip to content

h2d.Graphics accuracy #776

@ncannasse

Description

@ncannasse

Using the following sample, I was about to see that h2d.Graphics was not very accurate when drawing lines using various primitives:

  • drawRect is filling correctly (0,0,w,h) with fill but lines are (-1,-1,w,h)
  • drawRoundRect is missing some lines, must be an issue with line calculus
  • when we have a small offset <1 (O key) the lines are getting outside of the filled shape (green pixels)
  • circle/ellipsis seems ok
  • scaling seems ok
  • didn't check on JS (only HL)
class Main extends hxd.App {

	var tex : h3d.mat.Texture;
	var kind : Int;
	var evenSize = true;
	var pixelOffset = false;
	var MAX = 3;
	var scale = 0;

	override function init() {
		tex = new h3d.mat.Texture(255,255,[Target]);
		var bmp = new h2d.Bitmap(h2d.Tile.fromTexture(tex), s2d);
		bmp.x = 222;
		bmp.y = 111;
		bmp.scale(4);
		draw();
	}

	function draw() {
		var group = new h2d.Object();
		group.x = group.y = 4;
		group.scale(Math.pow(1.4,scale));

		if( pixelOffset ) {
			group.x += 0.5;
			group.y += 0.3;
		}

		var width = 63;
		var height = 17;

		if( evenSize ) {
			width++;
			height++;
		}

		if( kind == 2 ) height = width;

		var bmp = new h2d.Bitmap(h2d.Tile.fromColor(0x0000FF,width,height),group);

		var g = new h2d.Graphics(group);
		g.lineStyle(1 / group.scaleX, 0x00A000);
		g.beginFill(0xFF0000);
		switch( kind ) {
		case 0:
			g.drawRect(0,0,width,height);
		case 1:
			g.drawRoundedRect(0,0,width,height,4);
		case 2:
			g.drawCircle(width*0.5,height*0.5,width*0.5);
		case 3:
			g.drawEllipse(width*0.5,height*0.5,width*0.5,height*0.5);
		}
		g.blendMode = Add;

		tex.clear(0);
		group.drawTo(tex);
	}

	override function update(dt:Float) {
		super.update(dt);
		if( hxd.Key.isPressed(hxd.Key.LEFT) && kind > 0 ) {
			kind--;
			draw();
		}
		if( hxd.Key.isPressed(hxd.Key.RIGHT) && kind < MAX ) {
			kind++;
			draw();
		}
		if( hxd.Key.isPressed(hxd.Key.SPACE) ) {
			evenSize = !evenSize;
			draw();
		}
		if( hxd.Key.isPressed("O".code) ) {
			pixelOffset = !pixelOffset;
			draw();
		}
		if( hxd.Key.isPressed(hxd.Key.NUMPAD_ADD) ) {
			scale++;
			draw();
		}
		if( hxd.Key.isPressed(hxd.Key.NUMPAD_SUB) ) {
			scale--;
			draw();
		}
	}

	static function main() {
		new Main();
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions