Graphic arts. How to find out if a string is displayed on the screen based on its width

I make some basic graphs, and I wonder how I can know if the row will have some of its parts visible on the screen.

Take the line from x-5, y3 to x2, y-7. If it is 1 pixel wide, nothing will be displayed on the screen. If it is 15 pixels wide, some parts will be displayed.

How can I check this?

+4
source share
1 answer

If you only have strings, you can work with the function below. Otherwise, I would recommend walking the entire length of your line and creating a square of the line width at a certain distance and checking if it is in your view. Example. If you have a line from x0y0 to x7y0. You need to go to x1y0 to create a square of your drawing line size (in this example 15) and see if this screen overlaps your screen. Then go to x2y0 and so on. The advantage is that it will even work with Bezier curves (some information on the wiki, how Bezier will work).

// EDIT: (made a small bezier check function, should work, but not tested). And I don't think its more efficient performance for checking every line before drawing:

- (void)bezierWithStart:(CGPoint)start cp1:(CGPoint)cp1 cp2:(CGPoint)cp2 end:(CGPoint)end withWidth:(float)wid { for (float i = 0.0; i<=1.0; i+=0.05) { // how many steps CGPoint chk1 = CGPointMake(start.x+((cp1.x-start.x)*i), start.y+((cp1.y-start.y)*i)); CGPoint chk2 = CGPointMake(cp1.x+((cp2.x-cp1.x)*i), cp1.y+((cp2.y-cp1.y)*i)); CGPoint chk3 = CGPointMake(cp2.x+((end.x-cp2.x)*i), cp2.y+((end.y-cp2.y)*i)); CGPoint chk4 = CGPointMake(chk1.x+((chk2.x-chk1.x)*i), chk1.y+((chk2.y-chk1.y)*i)); CGPoint chk5 = CGPointMake(chk2.x+((chk3.x-chk2.x)*i), chk2.y+((chk3.y-chk2.y)*i)); CGPoint cPoint = CGPointMake(chk4.x+((chk5.x-chk4.x)*i), chk4.y+((chk5.y-chk4.y)*i)); CGRect drawLine = CGRectMake(cPoint.x-(wid/2), cPoint.y-(wid/2), wid, wid); // check if rect is in view } } 

// EDIT end

But now let's move on to a simple line function:

 - (void)testLine:(CGPoint)fp toSecond:(CGPoint)sp withWidth:(float)wid { float xratio = sp.x - fp.x; float yratio = sp.y - fp.y; double a = sqrt(((wid*wid)*(xratio*xratio))/((yratio*yratio)+(xratio*xratio))); a/=2; // because line width goes in both direction double b = (yratio/xratio)*a; if ((xratio<0.0 && yratio<0.0) || (xratio>0.0 && yratio>0.0))b*=-1; CGPoint diffFrom1 = CGPointMake(fp.x+a, fp.y+b); CGPoint diffTo1 = CGPointMake(sp.x+a, sp.y+b); a*=-1; b*=-1; CGPoint diffFrom2 = CGPointMake(fp.x+a, fp.y+b); CGPoint diffTo2 = CGPointMake(sp.x+a, sp.y+b); } 

you will get 4 points. 2 lines, one on top and one below the original line, half the size of your width. The calculation behind is getting the direction of the draw and for that the difference is with the original line. But for those who want to penetrate into it, this is my preliminary calculation:

enter image description here

+4
source

Source: https://habr.com/ru/post/1347254/


All Articles