[Rcpp-devel] wordcloud

Dirk Eddelbuettel edd at debian.org
Sat Jul 23 19:13:08 CEST 2011


On 23 July 2011 at 11:37, Dirk Eddelbuettel wrote:
| Ok, I'll be lazy now as I could just look at the code, but what type of
| layout operation did you move to C++? Is it a type of sorting / arranging /
| classifying / ... ?  Does it rely on other libraries or did you solve it with
| homegrown C++?  How many lines?

Ok, I can answer that myself as CRANberries keeps me a mirror of packages on
my disk anyway :) (and I see you answered now too -- thanks)

#import "Rcpp.h"

/*
 * Detect if a box at position (x11,y11), with width sw11 and height sh11 overlaps
 * with any of the boxes in boxes1
 */
using namespace Rcpp;

RcppExport SEXP is_overlap(SEXP x11,SEXP y11,SEXP sw11,SEXP sh11,SEXP boxes1){
	double x1 = as<double>(x11);
	double y1 =as<double>(y11);
	double sw1 = as<double>(sw11);
	double sh1 = as<double>(sh11);
	Rcpp::List boxes(boxes1);
	Rcpp::NumericVector bnds;
	double x2, y2, sw2, sh2;
	bool overlap= true;
	for (int i=0;i < boxes.size();i++) {
		bnds = boxes(i);
		x2 = bnds(0);
		y2 = bnds(1);
		sw2 = bnds(2);
		sh2 = bnds(3);
		if (x1 < x2)
			overlap = (x1 + sw1) > x2;
		else
			overlap = (x2 + sw2) > x1;


		if (y1 < y2)
			overlap = (overlap && ((y1 + sh1) > y2));
		else
			overlap = (overlap && ((y2 + sh2) > y1));

		if(overlap)
			return Rcpp::wrap(true);
	}

	return Rcpp::wrap(false);
}

That is indeed pretty nice and simple!

| And lastly ... given that also know Java so well: what works well / better
| with Rcpp for you?

I'd still be curious to hear about this one.

Dirk

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list