Hello I would like to calculate a weighted line integral. The integral is calculated by the cells that this lines trasverses (the small cells belong to matrix (m*n) that represent the value that a specific area has. I need to calculate the weights by finding out how much the line touches or impinges inside a cell. The weight is less if a line just touches one of the four edges of a square and much more if it traverses inside the cell. I think there might be some function to calculate this in any image processing packages. So far I could not find anything appropriate. Could you please help me with that? I would like to thank you in advance for your help Best Regards Alex [[alternative HTML version deleted]]
Hi Alex, I remember that you had the discussion recently about identifying grid cells that were crossed by a line... http://tolstoy.newcastle.edu.au/R/e11/help/10/09/8431.html So, following on from those posts, for any cell crossed by a line you will know the coordinates of the cell corners and the densified vertices of the line (referring to David W's approach of using a sequence of points along the line). You could then use the following simple function to get the area of the polygon defined by those coordinates... function(x, y=NULL) { # Calculate unsigned area of a simple (ie. no holes) polygon # # x, y vertex coordinates coords <- xy.coords(x, y) x <- coords$x y <- coords$y N <- length(x) if (!(x[1] == x[N] & y[1] == y[N])) { x <- c(x, x[1]) y <- c(y, y[1]) N <- N + 1 } px <- sum(x[-N] * y[-1]) py <- sum(y[-N] * x[-1]) abs((px - py) / 2) } The position of your cells and lines in the real number plane will determine which cell corners you include with the line vertices to calculate an area appropriate for your integral. Michael On 6 October 2010 01:31, Alaios <alaios at yahoo.com> wrote:> Hello > I would like to calculate a weighted line integral. > The integral is calculated by the cells that this lines trasverses (the small > cells belong to ?matrix (m*n) that represent the value that a specific area has. > > I need to calculate the weights by finding out how much the line touches or > impinges inside a cell. > The weight is less if a line just touches one of the four edges of a square and > much more if it traverses inside the cell. I think there might be some function > to calculate this in any image processing packages. > So far I could not find anything appropriate. > Could you please help me with that? > > I would like to thank you in advance for your help > Best Regards > Alex > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I would like to thank you for your reply. Yes I had this conversation of how to find the cells that are touched. I did that with these two lines: temp<-(floor(cbind(seq(x[1],xr[1],by=0.01),lineeq(x,xr)))) #. cellid2 <-unique( floor(cbind(seq(x[1],xr[1], by=0.01), lineeq(x,xr)) ) ) # cell ids that are touched You can find in the picture below how the cells look like for a line that spans from (2,11) to (3,8) http://img824.imageshack.us/img824/9914/lineu.jpg In the picture attached you can see that there are some points missing denoted by * which I do not know how to find out. I need them to find the proportion a line gets into each cell. You mentioned something like "densified vertices of the line" but I am not sure what does it mean. In the picture are depicted the areas that I need to calculate somehow. Of course the easiest way would be to use some already implemented function which does not seem to exist. I would like to thank everyone that contributed to this so far. Best Regards Alex -- View this message in context: http://r.789695.n4.nabble.com/Linear-Integration-tp2956145p2964694.html Sent from the R help mailing list archive at Nabble.com.
Hi Alex,> In the picture are depicted the areas that I need to calculate somehow. Of > course the easiest way would be to use some already implemented function > which does not seem to exist.That was why I posted a function to calculate polygon area in my previous reply :) Have you tried that approach ? Michael On 6 October 2010 22:21, alaios <alaios at yahoo.com> wrote:> > I would like to thank you for your reply. > Yes I had this conversation of how to find the cells that are touched. > > > I did that with these two lines: > temp<-(floor(cbind(seq(x[1],xr[1],by=0.01),lineeq(x,xr)))) #. > cellid2 <-unique( floor(cbind(seq(x[1],xr[1], by=0.01), lineeq(x,xr)) ) ) # > cell ids that are touched > > You can find in the picture below how the cells look like for a line that > spans from (2,11) to (3,8) > > http://img824.imageshack.us/img824/9914/lineu.jpg > > In the picture attached you can see that there are some points missing > denoted by * which I do not know how to find out. I need them to find the > proportion a line gets into each cell. > You mentioned something like "densified vertices of the line" but I am not > sure what does it mean. > > In the picture are depicted the areas that I need to calculate somehow. Of > course the easiest way would be to use some already implemented function > which does not seem to exist. > > I would like to thank everyone that contributed to this so far. > Best Regards > Alex > > -- > View this message in context: http://r.789695.n4.nabble.com/Linear-Integration-tp2956145p2964694.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
No, because I thought something that might be easier. If you see the image again you might notice that the proportions I am looking for might also be found by using the hypotenuse which is the same (as all squares are triangles) by finding the adjacent. The adjacent are easier to be found by tracking when the x value changes. This happens when the x value increments by one and when y also increments by one. As I know the line equation y=a*x+b it is very easy all these points. Then it is straightforward to find the proportion of these vectors? Best Regards Alex -- View this message in context: http://r.789695.n4.nabble.com/Linear-Integration-tp2956145p2966318.html Sent from the R help mailing list archive at Nabble.com.