Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Adjust thickness of a line  (Read 7045 times)

0 Members and 1 Guest are viewing this topic.

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« on: July 13, 2010, 08:21:50 pm »
As far as I can tell, there is no way of altering the thickness of a shape once it has been set. Is this true?

My program involves an awful lot of zooming in and out from a 1:1 view to a 1:4 view on occasion, which means that the line thicknesses need to be altered quite often (thickness 1 works fine when zoomed in, but "dissipates" when zoomed out, in which case thickness 4 works best. However, thickness 4 on 1:1 is very thick).

Would I therefore have to regenerate any existing shapes each time there is a view change, i.e.:
Code: [Select]
//considering the existence of a previous sf::Shape a, a line of thickness 1
sf::Shape b = sf::Shape::Line(a.GetPointPosition(1),a.GetPointPosition(2),4,sf::Color(0,128,128));


I was hoping there was a simple a.Thickness(4) which would change the property after a RenderWindow::Draw(a);

Otto

  • Newbie
  • *
  • Posts: 2
    • View Profile
Adjust thickness of a line
« Reply #1 on: July 13, 2010, 08:40:47 pm »
void Shape::SetOutlineWidth  (float width) does what you want.

So...

Code: [Select]

sf::Shape a;
...
a.SetOutlineWidth(4.f);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Adjust thickness of a line
« Reply #2 on: July 14, 2010, 11:18:19 am »
A sf::Shape is just a polygon made of custom points. There's no thickness, radius, etc.
The sf::Shape::Line, sf::Shape::Circle and sf::Rectangle functions are just helpers to create the most common shapes easily, without having to calculate the points manually.

However there are two easy solutions to your problem:
- Use a line with height == 0 and play with SetOutlineWidth
- Play with SetScale
Laurent Gomila - SFML developer

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« Reply #3 on: July 14, 2010, 04:05:00 pm »
Thanks guys.

SetOutlineWidth works great, but I'm just having a bit of trouble with the following. Whenever I use view::zoom(), all my continuous lines dissipate, as I said earlier, to the extent of the image becoming like this:


(Sorry for the large image, but resizing it makes most of the visible lines disappear)

Using SetScale instead (with the thickness changing by 1/zoom-factor, so that the lines get thicker if you zoom out and thinner if you zoom in), the problem is reduced, but still quite visible.

Is there a trick to avoid this?

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« Reply #4 on: July 14, 2010, 05:52:41 pm »
Sorry for the double post, but I just realized what I wrote was quite wrong.

SetScale is actually not very useful in this case because the scale is defined in the global X and Y. This means that I can't alter the scale to make the line thicker without also deforming it length-wise (for any inclined line). Well, I could, but that would require a fair share of trigonometry, which isn't friendly to one's processing speed.

It would seem one must use SetOutlineWidth in this case so that the deformation is perpendicular to the line itself.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Adjust thickness of a line
« Reply #5 on: July 14, 2010, 06:37:49 pm »
If the thickness of your lines is less than 1 pixel, there's nothing you can do to prevent such artifacts.

Maybe you can get better results with anti-aliasing?

You can also adjust the thickness when you zoom out, to get a global thickness of not less than 1 pixel.
Laurent Gomila - SFML developer

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« Reply #6 on: July 22, 2010, 07:09:42 pm »
Sorry for returning to this problem, but I've finally been able to sit down and (try to) work on this program again, so I've got new doubts.

I tried the following code:
Code: [Select]
float t = CurrentView.GetHalfSize().x/App.GetDefaultView().GetHalfSize().x;
shape.SetOutlineWidth(t);


It is my understanding that this would give me the "ratio" between the current view and the default, where thickness = 1 implies thickness = 1 pixel. This way, should CurrentView be zoomed out from DefaultView (by half), then the thickness would have to be 2. And since CurrentView's HalfSize is double DefaultView's, this should work, right?

I mean, I've tried it and it doesn't, but...

Obs: Playing with it, I just tried doing that with .SetOutlineWidth(20*t) and almost everything vanished. Only a few line-segments remained...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Adjust thickness of a line
« Reply #7 on: July 22, 2010, 07:43:34 pm »
Can you provide a minimal and complete source code, so that I can test it?
Laurent Gomila - SFML developer

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« Reply #8 on: July 22, 2010, 10:34:41 pm »
Here you go. Just press any key to zoom out. It takes a few steps for it to start to fade until finally vanishing far too early. And I apologize for all the global variables, I promise my real code isn't like this. :p

Also, I'm using SFML1.6.

Code: [Select]
#include <SFML/Graphics.hpp>
sf::RenderWindow App(sf::VideoMode(100,100),"TEST");
sf::View view;
sf::Shape shape = sf::Shape::Line(0,0,100,50,1,sf::Color(0,255,255));

void KeyPressed(sf::Event::KeyEvent e)
{
float t;
view = App.GetView();
view.Zoom(.9f);
App.SetView(view);
t=App.GetView().GetHalfSize().x/App.GetDefaultView().GetHalfSize().x;
shape.SetOutlineWidth(t);
}

int main()
{
sf::Event e;
sf::View View;
while(App.IsOpened())
{
App.Clear();
App.Draw(shape);
App.Display();
while(App.GetEvent(e))
{
if(e.Type == sf::Event::KeyPressed)
KeyPressed(e.Key);
if(e.Type == sf::Event::Closed)
App.Close();
}
}
return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Adjust thickness of a line
« Reply #9 on: July 22, 2010, 10:56:43 pm »
Well, it works but you just don't see it yet ;)

The outline color of your shape is black like your background, so you never see the outline.

Code: [Select]
sf::Shape shape = sf::Shape::Line(0,0,100,50,1,sf::Color(0,255,255), 1, sf::Color(255, 0, 0));
Laurent Gomila - SFML developer

wasabi

  • Newbie
  • *
  • Posts: 23
    • View Profile
Adjust thickness of a line
« Reply #10 on: July 22, 2010, 11:00:27 pm »
I hate myself.

But thank you. :p

Hugo

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Adjust thickness of a line
« Reply #11 on: July 23, 2010, 02:16:45 am »
I hahahahahahahahahaha-ed on that one  :D