Welcome to PhoneASK, where you can ask questions and receive answers from other members of the community.

Draw with arbitrary shaped pen

asked 一月 10 at 16:23 by The Lazy Coder

I need to draw lines onto a bitmap using a custom aperture. In the example below, there are two lines. The left line is horizontal. The right line is diagonal - down and to the right: Example image In the example image, the aperture and the path is shown in red. The resulting geometry is shown in black. Both of the above examples use the same aperture, but wipe it along a different path.

I'd like to be able to draw with an arbitrary shaped pen, even though the above example only shows rectangles.

I made an attempt at using the System.Drawing.2D namespace but it doesn't seem to do what I need it to do. Using custom end-caps on pens seemed promising, but the end caps rotate with the direction of the line. Also, getting the line-width right seems challenging.failed end-caps example

I've considered drawing the aperture over and over again, centered at different points along the path, but that doesn't seem performant. It is also quite difficult to minimize the number of aperture draws.

The best idea I've got is to try to draw the 'line' as a filled shape. At first, I thought a convex hull algorithm would be the answer - simply take the vertices of the aperture at the beginning of the draw and at the end of the draw and put them through a convex hull algorithm to find the 'outer' vertices. This works for my 1st example above, - but a star-shaped aperture demonstrates that this solution is incomplete. It only works when the aperture is convex itself. enter image description here

Simply putting all of the vertices through a convex hull algorithm would result in the areas highlighted in blue being filled in, but I need to only fill in the black areas.

Your answer

2 Answers

answered 一月 10 at 20:00 by Steve Tauber

This is an ugly, inelegant solution, but you could take the vector that represents the stroke direction and length and redraw your shape many times along that vector. (You'd have to choose an interval at which to draw it that didn't leave gaps.) This would only produce correct results if you want aliased output; it would screw up any anti-aliasing done by GDI+, because pixels with midtones would potentially be drawn over, making them darker than they should be.

answered 一月 10 at 22:52 by The Lazy Coder

The best idea I've got involves some redundant drawing, but I think it will work.

Treat the aperture polygon as a list of line segments. For each line segment in the list, draw a parallelogram using the line segment of the aperture at the start of the move, and using the same line segment of the aperture at the end of the move Parallelogram Draw

The above image shows this for three of the ten line segments that make up the star. For some pen shapes, there is potentially a lot of overlap between segments, but this method should result in all of the correct pixels being filled in. Notice that most of the green area is already filled in by the blue area and the red area, with just a small section in the lower-right star that's green-only. Redundantly filling in these pixel is wasteful, but will result in the correct image, I think.

Other Thoughts:

There may be a way to do this without redundantly filling in the same area, and it probably involves ray casting. Notice how the intersection of the red and green line in the lower-right would fall on a vertex that is not part of the original aperture. You would need to draw a ray from the right-most vertex of the right polygon along the direction of the move and find where that intersects the green line in order to draw the final shape correctly.

Related Questions

I tried to change the shape of some Android components like Button or View. What I can do is assign a shape to the background with the onDraw-Method. Well, it looks like reshaped, but the touch-events
I am trying to create a custom shaped dialog in android. What I want is instead of it being rectangularly shaped, to have any shape I might want to create. Like put a custom background which is a png
Is it possible on iOS to make a regular UIView in some CGRect and add subviews to it and then tell that container UIView something like this: containerView.layer.path = someClosedUIBezierPath ? And w
Is it possible to create the following shape as a DIV in CSS. The browser support is not important. Regards
In my android application activity, I need to arrange 6 buttons as shown below: The buttons are named from 1 to 6. When I try to add each button as background to the buttons, there is a problem that t
Here I created the image with round shape. But it covers all corners. I want affect only in left corners. CODEPEN: CODEPEN DEMO .circular { border-radius: 5px; -webkit-border-radius: 150px; -moz-borde
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawrectangle.aspx FillRectangle, DrawRectangle, FillElipse and DrawEllipse all can take 4 Float (or Single) parameters: x, y, width,
I am interested in implementing a user interface navigation control mechanism based on a wheel shaped device (or pie chart shaped or circle shaped) in my Android app. I would like only half of the whe
I have an image and I am using OpenCV to find the contours within that image. For example: contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) Each contour that get
I was wondering if anybody could point me to the best algorithm/heuristic which will fit my particular polygon packing problem. I am given a single polygon as a boundary (convex or concave may also co
We're hosting Cassandra 2.0.2 cluster on AWS. We've recently started upgrading from normal to SSD drives, by bootstrapping new and decommissioning old nodes. It went fairly well, aside from two nodes
I need a way to characterize the size of sets of 2-D points, so I can determine whether to render them as individual points in a space or as representative polygons, dependent on the scale of the view
I have some irregular shaped buttons, created as ImageButtons. The android:src attribute of the ImageButtons are .PNG files with transparent backgrounds. And the parent layout of these ImageButtons,
I have received a design which i am pretty sure is a flash website yet the company is convinced that it is doable in html, the design Any feedback would be nice.
I'm writing a control that should be able to display any list of data. What I wanted to do, was to mimic the for-in construct in that I check for a public GetEnumerator function that contain a Current
I would like to generate Arbitrary value for ordered tree-like structure whose type is, say data Tree a = Leaf | Node (Tree a) a (Tree a) A function that inserts value into this tree while keeping i
I have users stored in the database and need to save a timezone to associate with the user so that I can use it to create events with AddThisEvent and to display other localized times. Is the mapping
Is it possible to set a conditional breakpoint in GDB where the the condition expression contains objects of arbitrary class types? I need to set a breakpoint inside a function where the condition wil
Just to clarify the title, I have an XML document that looks like the following: <group> <starter>Start</starter> <backup1>Backup1</backup1> </group> <group>
This should be an easy task, but I'm having a hard time getting it to work in Sympy. I want to substitute an undefined function with arbitrary arguments with a specific formula for example: from sympy
I am currently using juggling db with NodeJS for ORM. I now need to do some reporting, which involves joining data from several tables using arbitrary SQL with SUM and GROUP BY too. How would I do thi
I have to set image in circular button, Image can be picked from gallery or captured by camera. Now when i tried to set portrait or Landscape image on my button.imagview with AspectFit Content Mode th
I am trying to figure out how to have a UI design on my main window application that looks like this: The base object is a DockPanel - I know I can set the DockPanel background to set a graphic there
How can I remove an arbitrary item from a priority queue. Suppose I have a PriorityQueue for jobs. I have a job I want to cancel so I need to remove it from the queue, how can I do that? UPDATE To a
Standard lesscss mixin: .box-shadow(@val) { -moz-box-shadow: @val; box-shadow: @val; } However, in pure CSS I'm able to use several box shadows on one element, e.g. #myBox { box-shadow: inset 0px 1px
Question Is there a way to create a arbitrary size integer using c/c++? For example: int main(void) { Int i = Int(3); //3-bit integer i = 1; //Represented as: 001 } Bonus Is there a way to do the
I'd like to do this without scraping the page, even if there's a way that isn't strictly universal. After some looking around, I found that you can get the last-modified date from the headers, but I
I am writing a text-editor and I would need to store a few pieces of information (generally just a few strings; the storage needn't be particularly durable) with each file the app saves (without that
I'm trying to generate JSON objects in the form of {{s:v1, t:v2}, {s:v3, t:v4}, etc} via embedded structs in Go. It all works out fine when all Problem items in type Problems []Proble
I want to create an arbitrary number of droplets when I call a playbook with ansible. For example: I need to create 10 droplets running some python code. $ ansible-playbook install_pyapp_commission_ne
for an assignment I have to generate a truth table like this: combinations :: Int -> [[Bool]] combinations 3 should output: [[False, False, False],[False, False, True],[False, True, False],[False,
I can do a two level nested loop like this for i1 in 1:n for i2 in 1:n do something with (i1,i2) How do I extend this into arbitrary level of nested loop? For example, I can do this in Python to loop
I have a Matlab structure with varying depths, that is something like: f.A = 1; f.B.alpha = 20; f.B.beta = 30; Now suppose you have a given input (say 'ctrl') that specifies a value to be reassigned,
I'm working on a logging app in Django to record when models in other apps are created, changed, or deleted. All I really need to record is the user who did it, a timestamp, a type of action, and the
I am trying to write an XSLT 2.0 function that returns a result of a specific type--let's say one or more elements. Here's what I've tried, to no avail: <xsl:function name=util:find-parents2 as=
What I am actually doing is more complex but it comes down to being able to implement function to detect that something is a tuple, regardless of what the are the types of its elements. This is my app
p = subprocess.Popen(args = myprog.exe + + str(input1) + + str(input2) + + str(input3) + + strpoints, stdout = subprocess.PIPE) in the code above, input1, input2, and input3 are al
I need to do the following in JavaScript and so far been unable to find solutions to do it seamlessly: Grab two integers in a specific order and pack them like Python's struct module. This packed val
I'm contributing to a javascript framework which has the equivalent of the following code: eval(' + user_input.replace(/'/g, &#39;) + '); I know this is terrible -- no need to persuade me.
In base graphics I can create a 4 panel pane of graphics by doing the following: par(mfrow=c(2,2)) for (i in 1:4){ plot(density(rnorm(100))) } which results in I'd like to do the same sort of thing
I am using the fluent interface to create a Zend DB Select object/query. As part of the query, I would like to select an arbitrary string, like SELECT 'foo' AS 'type' FROM .... foo is not a column,
say that I have a (40,20,30) numpy array and that I have a function that after some work will return half of the input array along a selected input axis. Is there an automatic way to do so ? I would l
In short, I want to declare a trait like this: trait Test { def test(amount: Int): A[Int] // where A must be a Monad } so that I can use it without knowing what monad that A is, like: class Usecase
I had a batch file, which (when simplified) looked like this: @Echo Off SetLocal EnableDelayedExpansion MD MyProgram^! MD MyProgram version 2 MD MyProgram (next version) MD MyProgram O&O D
Is there a data structure of combination of structures ( STL, Boost etc. ) that would allow me to sort data in memory like it is done in this table here http://en.wikipedia.org/wiki/List_of_cities_pro
I have some graphic files with some rather long filenames which includes several periods. includegraphics interprets the first of these as the beginning of the file extension, which makes it impossibl
I want to link in raw binary data. I'd like to either put it at a particular address, or have it link to a symbol (char* mydata, for instance) I have defined in code. Since it's not an obj file, I can
(This algorithm is for an iPhone app I am working on, if that helps the context at all.) We need to make UUIDs to uniquely identify some products. Usually this is as simple as assigning unique numbers
I would like to draw a centered multiline NSString. Using - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment t
A lot of times when I draw numbers I get 0 and all the time I can see similar draw sequences. Do you know why? For example: losowanie(3) -> result 0 some times 1? Code: public int losowanie(int w)